感謝您閱讀本文並提前給予幫助。包含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讓我知道! *(=
爲重複的代碼塊創建一個函數。 – 2014-09-04 17:48:29