2014-09-04 165 views
0

感谢您阅读本文并提前给予帮助。包含PHP中的文件

我得到了一个简单的书目/程序代码/,每个访问者都可以看到图书目录并点击书籍并查看他们的作者,但是我有一个想法来添加一个功能,允许用户编写只有在他们登录后才对每本书发表评论。所以我添加了一个会话变量$_SESSION['isLogged']。但是有很多重复的代码块。我需要的是一个建议,如何处理这些重复的代码块。好的做法说的是什么?我的代码来自我得到的6个文件之一。在每个文件中,我都得到了重复的代码。 因此,这里是我的代码:

if (!isset($_SESSION['isLogged'])) { 
    echo '<div class="user-navigation"> 
      <a href="register.php" class="user-nav" >Register now</a> 
      <a href="login.php" class="user-nav">Log in</a> 
      </div> 
      <div class="navigation1"> 
      <a href="new-book.php" class="nav">Add book</a> 
      <a href="new-author.php" class="nav">Add author</a> 
      </div>'; 

    $booksAndAuthors = mysqli_query($connection, 'SELECT DISTINCT * FROM books LEFT JOIN books_authors ON books.book_id=books_authors.book_id LEFT JOIN authors ON authors.author_id=books_authors.author_id'); 
    $result = array(); 
    while ($resultArr = mysqli_fetch_assoc($booksAndAuthors)) { 
     $result[$resultArr['book_id']] ['book_name'] = $resultArr['book_title']; // Reodering array 
     $result[$resultArr['book_id']] ['author'][$resultArr['author_id']] = $resultArr['author_name']; // Reordering array 
    } 


    echo '<table class="table"><tr><th>Book name</th><th>Author</th></tr>'; // Open table html table tags 
    foreach ($result as $k=>$b) { // Foreach the result array to get the book_name 
     echo '<tr><td><a href="book-comment.php?book_id='.$k.'">' . $b['book_name'] . '</a></td><td>'; 
     $data = array(); // Create an empty array in order to fill the data inside 

     foreach ($b['author'] as $k => $a) { // Foreach the nested array with the authors to get the author_name displayed 
      $_GET['author_name'] = $a; 
      $data[] = '<a href="books_of_an_author.php?author_id=' . $k . '" class="author_link">' . $a . '</a>'; // Link is chosen by the author_id 
     } 
     echo implode(', ', $data); // Add a comma after every record 
     echo '</td></tr>'; // Close table cell and row 
    } 
     exit; 
    echo '</table>'; // Close html table tag 
} 

else { 
    echo '<div class="user-navigation"> 
      <a href="profile.php" class="user-nav">My Profile</a> 
      <a href="logout.php" class="user-nav">Log out</a> 
      </div> 

      <div class="navigation2"> 
      <a href="new-book.php" class="nav">Add book</a> 
      <a href="new-author.php" class="nav">Add author</a> 
      </div>'; 

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    $booksAndAuthors = mysqli_query($connection, 'SELECT DISTINCT * FROM books LEFT JOIN books_authors ON books.book_id=books_authors.book_id LEFT JOIN authors ON authors.author_id=books_authors.author_id'); 
    $result = array(); 
    while ($resultArr = mysqli_fetch_assoc($booksAndAuthors)) { 
     $result[$resultArr['book_id']] ['book_name'] = $resultArr['book_title']; // Reodering array 
     $result[$resultArr['book_id']] ['author'][$resultArr['author_id']] = $resultArr['author_name']; // Reordering array 
    } 
    echo '<table class="table"><tr><th>Book name</th><th>Author</th></tr>'; // Open table html table tags 
    foreach ($result as $k=>$b) { // Foreach the result array to get the book_name 
     echo '<tr><td><a href="book-comment.php?book_id='.$k.'">' . $b['book_name'] . '</a></td><td>'; 
     $data = array(); // Create an empty array in order to fill the data inside 
     foreach ($b['author'] as $k => $a) { // Foreach the nested array with the authors to get the author_name displayed 
      $_GET['author_name'] = $a; 
      $data[] = '<a href="books_of_an_author.php?author_id=' . $k . '" class="author_link">' . $a . '</a>'; // Link is chosen by the author_id 
     } 
     echo implode(', ', $data); // Add a comma after every record 
     echo '</td></tr>'; // Close table cell and row 
    } 
    echo '</table>'; // Close html table tag 
} 

*如果我的问题是不正确的问,或者你有一些notices.Please让我知道! *(=

+0

为重复的代码块创建一个函数。 – 2014-09-04 17:48:29

回答

0

放在一个文件这个代码,然后包括所有的页面的底部,该文件。

例如把代码isLogged.php

又在哪里代码被复制,取而代之的是每一页上:

include "isLogged.php"; 
+0

我想知道现在我确定。谢谢! – noSpoon 2014-09-04 18:21:06

0

创建a function of repeated block和打电话的时候,建立一个档案,设定重复的代码,并使用所需的任何orinclude,include_once,require_once添加文件。更多详细信息请参阅php手册了解这些功能。