doudao2407 2015-06-02 20:34
浏览 121

在PHP页面中执行代码并回显HTML输出

I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.

Here is the problem. I am creating a templated php website. With templated I mean something like below:

<?php include("header.php");?>
<div id="content">
    <div id="main">
        <h2><?php echo($page_title);?></h2>
        <?php 
            echo ($page_content);
        ?>
    </div>
    <?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>

As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page. To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:

$page_content = file_get_contents("page1_content.txt");

My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:

$page_content = file_get_contents("page2_content.php");

Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.

How can I achieve this? Please ask me any questions if you need more info/clarification.

Thanks

EDİT: As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.

Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)

</div>
  • 写回答

3条回答 默认 最新

  • dqde43215 2015-06-02 20:38
    关注

    I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).

    echo($page_content);
    

    Would become as below:

    include("page2_content.php");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题