在$ content母版页上显示PHP页

时间:2018-07-31 04:53:58

标签: php

我想就我现在面临的问题寻求帮助。 我有一个包含以下代码的母版页(mas​​ter.php)。

<html>
    <head>
        <meta charset="UTF-8">
        <title>Master Page</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

        <!-- jQuery library -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

        <!-- Popper JS -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>

        <!-- Latest compiled JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    </head>
    <body>
        <!--Header -->
        <div class="jumbotron text-center" style="margin-bottom:0">
            <h3>Kiribati Infrastructure Database System</h3>
        </div>
        <!--Main Menu -->

        <!--Main Content -->
            <div><?php echo $content;?></div>
        <!--Footer -->        
        <div class="jumbotron text-center" style="margin-bottom:0">
            <p>Footer</p>
        </div>
    </body>
</html>

每个php页面都将具有以下php编码

 <?php
         $content = '';
    include('master.php');
    ?>

我现在面临的问题是我无法显示其他php页面 进入那个变量。

例如,我想显示此编码的结果并将其放入 $ content变量,但我不知道如何。我将感谢为该项目提供的任何帮助。

    <body>
            <div class="container">
                            <h2>Gender Report:</h2>
                            <p></p>            
                            <table class="table">
                              <thead>
                                <tr>
                                  <th>ID</th>
                                  <th>Gender</th>
                                </tr>
                              </thead>
                              <tbody>


            <?php
            // Provide the file for username, password and database name
             require_once 'mysqlconn.php';
              $conn = new mysqli($hostname, $username, $password, $database);

              if ($conn->connect_error) {
                  die("Connection failed:" . $conn->connect_error);
              }

              $sql = "SELECT * FROM tbl_gender";
              $result = $conn->query($sql);
                       if ($result->num_rows > 0) {
                  while ($row = $result->fetch_assoc()) 
                  {

                      echo "<tr>";
                      echo "<td>" . $row['gender_id'] . "</td>";
                      echo "<td>" . $row['gender_description'] . "</td>";
                      echo "</tr>";
                  }
              } else {
                  echo "0 results";
              }
              $conn->close();
              //include('master.php');
            ?>
                              </tbody>
                            </table>
            </div>                
   </body>

1 个答案:

答案 0 :(得分:0)

您可以使用输出缓冲来捕获变量内php脚本的内容。通过打开输出缓冲来启动“内容”文件,通过关闭输出缓冲并将缓冲区的内容分配给变量来结束文件。

<?php ob_start(); ?>

--php script which generate html content here--

<?php $content = ob_get_clean(); ?>
相关问题