2017-04-14 71 views
0

我有一個html和php代碼(下面)和一個template.php,我在其中定義了一個可變名稱 - $ content和$ title以及我創建的每一頁我想不同地定義這些變量和include template.php,所有頁面的設計都一樣。將一個html和php代碼堆棧在一個php變量中

HTML和PHP代碼 - indexForum.php:

<?php 
    session_start(); 
    $db=mysqli_connect("localhost","root","","travelersdb"); 
    if(@$_SESSION["username"]){ 
     ?> 

    <?php 
    // function to connect and execute the query 
    function filterTable($query) 
    { 
     $connect=mysqli_connect("localhost","root","","travelersdb"); 
     $filter_Result = mysqli_query($connect, $query); 
     return $filter_Result; 
    } 
    if(isset($_POST['search'])) 
    { 
     $valueToSearch = $_POST['valueToSearch']; 
     $valueToSearchTwo = $_POST['valueToSearchTwo']; 
     // search in all table columns 
     $query = "SELECT * FROM `topics` WHERE (topic_creator = '".$valueToSearch."' OR topic_id='".$valueToSearchTwo."') "; 
     $search_result = filterTable($query); 
    } 

    else { 
     $query = "SELECT * FROM `topics`"; 
     $search_result = filterTable($query); 
    } 
    ?> 


    <html> 
     <head> 
      <title>Home Page</title>  
     <body> 

     </body> 
     </head> 
     <?php include("header.php");?> 
     <center> 
      </br> 
      <a href ="post.php"><button>Post</button></a> 
      </br> 
      <br/> 
      <form action="indexForum.php" method="post"> 
       <input type="text" name="valueToSearch" placeholder="topic_creator"><br><br> 
       <input type="text" name="valueToSearchTwo" placeholder="id"><br><br> 
       <input type="submit" name="search" value="serach"><br><br> 
      <br/> 
      <?php echo '<table border="1px">';?> 
       <tr> 
        <td> 
        <span>ID</span> 
        <td width="400px;" style="text-align:center;"> 
        Pic 
        </td> 
        <td width="400px;" style="text-align:center;"> 
        Name 
        </td> 
        <td width="80px;" style="text-align: center;"> 
         Views 
        </td> 
        <td width="80px;" style="text-align: center;"> 
         Creator 
        </td> 
        <td width="80px;" style="text-align: center;"> 
         Date 
        </td> 
      </tr> 
     </center> 
     <body> 
     </body> 
    </html> 


    <?php 
    $sql = "Select * from topics"; 
    $check = mysqli_query($db,$sql); 
    $rows = mysqli_num_rows($search_result); 



    if($rows != 0){ 
     while ($row = mysqli_fetch_assoc($search_result)){ 
      $id = $row['topic_id']; 
      echo "<tr>"; 
      echo "<td>".$row['topic_id']."</td>"; 
      echo "<td><image src='".$row['topic_pic']."' width='60' height='60'></td>"; 
      echo "<td style='text-align:center;'><a href='topic.php?id=$id'>".$row['topic_name']."</a></td>"; 
      echo "<td>".$row['views']."</td>"; 
      $sql_u = "Select * from users where username='".$row['topic_creator']."'"; 
      $check_u = mysqli_query($db,$sql_u); 
       while ($row_u = mysqli_fetch_assoc($check_u)) 
           { 
            $user_id = $row_u['id']; 
           } 
      echo "<td><a href='profile.php?id=$user_id'>".$row['topic_creator']."</a></td>"; 
      $get_date = $row['date']; 
      echo "<td>".$row['date']."</td>"; 
      echo "</tr>"; 
     } 
    }else{ 
      echo "No topics found"; 
     } 
    echo "</table>"; 


    if (@$_GET['action']=="logout"){ 
     session_destroy(); 
     header('location:login.php'); 
    } 
    }else 
     { 

     echo "You must be logged in."; 
     ?> 
    <a href ="index.php">Home Page</a><br/></br> 
    <a href ="login.php">Login</a> 
    <?php header('location:indexForum.php');?> 
    <?php 
    } 

    ?> 

<?php include template.php ?> 

的template.php:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title><?php echo $title; ?></title> ------------------- 
     <link rel="stylesheet" type="text/css" href="Styles/Stylesheet.css" /> 
    </head> 
    <body> 
     <div id="wrapper"> 
      <div id="banner">    
      </div> 

      <nav id="navigation"> 
       <ul id="nav"> 
        <li><a href="index.php">Home</a></li> 
        <li><a href="topics.php">Coffee</a></li> 
        <li><a href="#">Shop</a></li> 
        <li><a href="#">About</a></li> 
        <li><a href="Management.php">Management</a></li> 
       </ul> 
      </nav> 

      <div id="content_area"> 
       <?php echo $content; ?> --------- 
      </div> 

      <div id="sidebar"> 

      </div> 

      <footer> 
       <p>All rights reserved</p> 
      </footer> 
     </div> 
    </body> 
</html> 

所以我的問題是,在一般情況下,我怎麼能定義indexForum.php所有代碼並將其堆疊到$ content變量中,以便在我編寫include template.php時,他會識別該變量,並且所有內容都將在模板中進行設計。謝謝!

回答

0

簡答:在包含模板文件之前設置$ title和$ content。我的意思是這裏沒有任何技巧。 PHP很簡單,你有變量和函數,你可以把它們叫做你想要的地方。你也可以有子模板和函數來調用這些模板(基本上包括需要的地方)。在此之前需要正確設置變量。

如果您事先發送內容主體(任何輸出,包括空白區域),那麼您的<?php header('location:indexForum.php');?>和其他Header()函數也不會起作用。你會得到「頭已發送」的錯誤。看到這個鏈接:How to fix "Headers already sent" error in PHP

相關問題