Pregunta

Here is my case. I have the following script:

<?php
// ... other code ...
switch($_GET['request'])    {
        case "firstPage":
            $file = APP_ROOT. "pages/firstPage.php";
            $hndle = fopen($file,"r");
            $right_column = fread($hndle, filesize($file));
            fclose($hndle);
            break;
        case "secondPage":
            $file = APP_ROOT. "pages/secondPage.php";
            $hndle = fopen($file,"r");
            $right_column = fread($hndle, filesize($file));
            fclose($hndle);
            break;
    }
}
?>
<div id="right_column">
    <?php echo $right_column;?>
</div>
// ... other code ...

Depending on the value of the $_GET['request'] I am assigning to the variable $right_column the content of a php file. Then, I echo that variable in the last div. The firstPage.php and secondPage.php files contain mixed html and php code. I look for a solution like the 'partial' in Zend. Thanks

¿Fue útil?

Solución

It would probably be simpler if you set a variable indicating which PHP file to include, and then rather than reading the file, just include it. For example:

<?php
// ... other code ...
switch($_GET['request'])    {
        case "firstPage":
            $file = APP_ROOT. "pages/firstPage.php";
            break;
        case "secondPage":
            $file = APP_ROOT. "pages/secondPage.php";
            break;
    }
}
?>
<div id="right_column">
    <?php include($file);?>
</div>
// ... other code ...

If your firstPage.php and secondPage.php files only had HTML, what you're doing would work. If it has PHP, then you'll need to include() it; or you could eval() it but that is again doing more work than you need.

Otros consejos

First of all , your code is horrible, it should be something like :

$pages = array(
   'firstPage'   => 'pages/firstPage.php',
   'secondPage'  => 'pages/secondPage.php'
);

$request = 'firstPage';
if ( array_key_exits($_GET, 'request') 
 &&  array_key_exists($pages, $_GET['request']) )
{
   $request = $_GET['request'];
}
?>
<div id="right_column">
    <?php include  APP_ROOT . $pages[ $request ]; ?>
</div>

An please read http://codeangel.org/articles/simple-php-template-engine.html , you might find this interesting. It should explain to you , how you can easily take advantage of php's native templating capabilities.

Actually, reading and dumping your file is not a solution. You are just displaying code to your end-user.

What you need is parse the PHP file and display the result. You can do it in 2 ways:

First, intead of

 $file = APP_ROOT. "pages/firstPage.php";
 $hndle = fopen($file,"r");
 $right_column = fread($hndle, filesize($file));
 fclose($hndle);

You can do:

$right_column = include(APP_ROOT . "pages/firstPage.php");

So, your firstPage.php must RETURN the code. Something like this:

// firstPage.php
return "my html";

But you also can include it like this:

<div id="right_column">
    <?php include(APP_ROOT . "pages/firstPage.php"); ?>
</div>

Or, you can use the ob_get_contents. PHP has a nice documentation about how to use it.

I'd use include for your case.

It seems to me that you could program by convention rather than have an ever increasing switch statement:

<?php
// ... other code ...

$file = '';
if (isset($_GET['request'])) {
    $safeFileName = sanitizeRequestForFileName($_GET['request']);
    $file = APP_ROOT. 'pages/' . $safeFileName . '.php';
}
?>
<div id="right_column">
    <?php 
        if (file_exists($file)) {
            include($file);
        }
    ?>
</div>
// ... other code ...
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top