将 PHP 和 HTML 代码加载到
中回显的 PHP 变量中。

发布于 2024-12-17 14:36:30 字数 846 浏览 1 评论 0原文

这是我的案例。我有以下脚本:

<?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 ...

根据 $_GET['request'] 的值,我将 php 文件的内容分配给变量 $right_column 。然后,我在最后一个 div 中回显该变量。 firstPage.php 和 secondaryPage.php 文件包含混合的 html 和 php 代码。我寻找像 Zend 中的“部分”这样的解决方案。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情释 2024-12-24 14:36:30

首先,你的代码很糟糕,它应该是这样的:

$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>

请阅读 http://codeangel.org/articles/simple-php-template- engine.html ,您可能会发现这很有趣。它应该向您解释如何轻松利用 php 的本机模板功能。

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.

捂风挽笑 2024-12-24 14:36:30

实际上,读取并转储文件并不是解决方案。您只是向最终用户显示代码。

您需要的是解析 PHP 文件并显示结果。您可以通过两种方式做到这一点:

首先,

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

您可以这样做:

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

因此,您的firstPage.php 必须返回代码。像这样的东西:

// firstPage.php
return "my html";

但您也可以像这样包含它:

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

或者,您可以使用ob_get_contents。 PHP 有一个关于如何使用它的很好的文档

我会用 include 来处理你的情况。

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.

柠栀 2024-12-24 14:36:30

如果您设置一个变量来指示要包含哪个 PHP 文件,然后不读取该文件,只需包含它,这可能会更简单。例如:

<?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 ...

如果您的firstPage.php 和secondPage.php 文件只有HTML,那么您所做的事情就可以工作。如果它有 PHP,那么您需要 include() 它;或者你可以eval()它,但这又做了比你需要的更多的工作。

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.

假装不在乎 2024-12-24 14:36:30

在我看来,您可以按照约定进行编程,而不是不断增加 switch 语句:

<?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 ...

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 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文