感谢您提前阅读本文并提供帮助。
我有一个简单的书籍目录/程序代码/,每个访问者都可以看到书籍目录并点击书籍并检查他们的作者但我有一个想法添加一个功能,允许用户写一个只有在记录时才对每本书发表评论。所以我添加了一个会话变量$_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
}
*如果我的问题没有得到正确询问或你有一些通知。请告诉我! *(=
答案 0 :(得分:0)
include是您正在寻找的。
答案 1 :(得分:0)
将此代码放入文件中,然后将该文件包含在所有页面的底部。
例如,将代码放在isLogged.php
中然后在每个代码重复的页面上将其替换为:
include "isLogged.php";
答案 2 :(得分:0)
您应该创建一个包含重复代码的文件。例如functions.php
。
然后用其中一种方法调用它。
include "functions.php"
http://php.net/manual/en/function.include.php
include_once("functions.php")
http://php.net/manual/en/function.include-once.php
require_once("functions.php")
答案 3 :(得分:0)
创建a function of repeated block
并在需要时调用or
使用重复代码集创建文件并使用任意include,include_once,require_once
添加文件。有关详细信息,请参阅php手册功能