dongzhao2725 2018-07-31 04:53
浏览 65
已采纳

在$ content主页面上显示PHP页面

I would like to seek assistance with the problem I'm facing now. I have a master page (master.php) with the following code.

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

Every php-page will have the following php codings

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

The problem I'm facing now is that I can't display other php-pages to get in that variable.

For example, I want to display the result of this codings and put it inside $content variable but I don't know how. I would appreciate any assistance provided for this project.

    <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条回答 默认 最新

  • douqian1835 2018-07-31 06:17
    关注

    You can use output buffering to capture the contents of a php script inside a variable. Start the 'content' file by turning on output buffering and end the file by turning off output buffering and assigning the contents of the buffer to your variable.

    <?php ob_start(); ?>
    
    --php script which generate html content here--
    
    <?php $content = ob_get_clean(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭