很少有人以前我开始学习PHP和MVC。所以我开始制作简单的项目并陷入分页问题。我需要帮助才能在我的网站中找到最佳的分页解决方案。在我发现的大多数分页示例中,使用SQL SELECT LIMIT进行分页,但在我的情况下,它似乎不太符合逻辑。
我有BooksController:
<?php
require 'Database/Connection.php';
require 'Database/QueryBuilder.php';
require 'Database/Book.php';
class BooksController
{
public function index()
{
$config = require 'config.php';
$books = new QueryBuilder(Connection::make($config['database']));
$books = $books->selectAll('books_table');
require 'views/listofbooks.view.php';
}
public function about()
{
$config = require 'config.php';
$books = new QueryBuilder(Connection::make($config['database']));
$bookId = $_REQUEST['id'];
$book = $books->selectByID('books_table', $bookId);
require 'views/aboutbook.view.php';
}
public function search()
{
$config = require 'config.php';
$books = new QueryBuilder(Connection::make($config['database']));
$text = $_REQUEST['search'];
$books = $books->search('books_table', $text);
require 'views/listofbooks.view.php';
}
}
和ListOfBooks查看:
<!DOCTYPE html>
<html>
<head>
<title>Books</title>
<link rel="stylesheet" type="text/css" href="styles/styles.css">
<script src="styles/sorttable.js"></script>
<script>sorttable.sort_alpha = function(a,b) { return a[0].localeCompare(b[0], 'lt'); }</script>
</head>
<body>
<form action="/search" name="search" method="GET">
<input type="text" id="textbox" size="30" name="search" placeholder="Text" required />
<input type="submit" id="button">
</form>
<table class="sortable">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Year</th>
<th scope="col">Author</th>
<th scope="col">Genre</th>
</tr>
</thead>
<tbody>
<?php foreach($books as $book) : ?>
<tr>
<td data-label="Title"><a href="/about?id=<?php echo $book->id; ?>"><?php echo $book->Title; ?></a></td>
<td data-label="Year"><?php echo $book->Year; ?></td>
<td data-label="Author(s)"><?php echo $book->Author; ?></td>
<td data-label="Genre"><?php echo $book->Genre; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
&#13;
我应该更改我的重新创建我的BooksController还是有其他任何解决方案?
答案 0 :(得分:1)
由于你没有提到它必须在PHP中完成,在我看来,对你的问题最简单的解决方案是一个简单的DataTable插件。它是javascript(确切地说是jQuery,但这是一个javascript框架)并且它将简化您的服务器的使用(每个分页页面不会有一个查询,但是一个查询和javascript将处理分页),但增加客户端PC的使用。此外,您还可以使用dataTables进行过滤,搜索等,但这是最简单的开始:https://datatables.net/examples/basic_init/zero_configuration.html