当前位置: 动力学知识库 > 问答 > 编程问答 >

Include files in PHP

问题描述:

Thank you for reading this and for your help in advance.

I got a simple Book-Catalogue /procedure code/, every visitor can see the book catalogue and click through out the books and check their authors BUT I've got an idea to add a feature that allows the users to write a comment to each book ONLY if they're logged. So I added a session variable $_SESSION['isLogged']. But there is a lot of code blocks that duplicates. What I need is an advice, what to do with this duplicated blocks of code. What the good practice says? And my code below is from one of the 6 files that I got. In everyfile I got this repeating of code.

So here's my code:

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

}

*If my question is not properly asked or you have some notices.Please let me know!

* (=

网友答案:

Put this code in a file then include that file at the bottom of all of the pages.

For example put the code in isLogged.php

Then on each page where the code is duplicated replace it with:

include "isLogged.php";
网友答案:

include is what you're looking for.

网友答案:

You should create a file that contains your repeated code. functions.php for example.

Then call it with one of these methods.

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")

http://php.net/manual/en/function.require-once.php

网友答案:

create a function of repeated block and call when it required or create a file with the set of repeated code and use any of include,include_once,require_once to add the file.for more detail see php manual for these functions.

分享给朋友:
您可能感兴趣的文章:
随机阅读: