2014-09-04 215 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手冊瞭解這些功能。