我正在为此制作一个新主题,因为我现在实际上在某个地方。
我有一个寻呼系统,但是有问题。
我与DAO和控制器合作。 事情是,我的第二页(我的分页系统)转到index.php?page = 2。这里的主要问题是,无论何时进入index.php中不存在的页面,它都会自动重定向到index.php。因为我不能在那里提到每一页,因为页面会增加插入的项目。
以下是我用于分页的整个代码:(已编辑)
<?php
$mysqli = new mysqli("localhost", "wanthave", "wanthavepass", "wanthave");
$count_mem = $mysqli->query("SELECT `id` FROM `wanthave_items`");
$rows = $count_mem->num_rows;
$perPage = 5; // items to be shown per page
$num_pages = ceil($rows / $perPage);
$visiblePages = 5; // if current page is 7 show: 5,6,7,8,9,...
$visibleOffset = ceil($visiblePages / 2) - 1; // number of pages to show on the left and on the right of the current page
// Where do you use this ???
// $start = $page == 1 ? 0 : (($page - 1) * $perPage);
if ($num_pages > 1) {
$first = $last = $pagesStr = '';
echo $first . $pageStr . $last;
}
foreach($items as $item) {
echo "<li>
<a href=\"index.php?page=item-detail&id={$item['id']}\">{$item['name']}</a>
</li>";
}
for ($n = 1; $n < ($num_pages + 1); $n++) {
echo "<a href='index.php?page=$n'>$n</a>";
if ($n < $num_pages) echo ", ";
}
?>
我的index.php
<?php
session_start();
define('DS', DIRECTORY_SEPARATOR);
define('WWW_ROOT', __DIR__ . DS);
$routes = array(
'home' => array(
'controller' => 'Item',
'action' => 'index'
),
'register' => array(
'controller' => 'Users',
'action' => 'register'
),
'login' => array(
'controller' => 'Users',
'action' => 'login'
),
'logout' => array(
'controller' => 'Users',
'action' => 'logout'
),
'item-detail' => array(
'controller' => 'Item',
'action' => 'detail'
),
);
if(empty($_GET['page'])) {
$_GET['page'] = 'home';
}
if(empty($routes[$_GET['page']])) {
header('Location: index.php');
exit();
}
$route = $routes[$_GET['page']];
$controllerName = $route['controller'] . 'Controller';
require_once WWW_ROOT . 'controller' . DS . $controllerName . ".php";
$controllerObj = new $controllerName();
$controllerObj->route = $route;
$controllerObj->filter();
$controllerObj->render();
答案 0 :(得分:0)
这是关于使用PHP Limit pagination page number
进行简单分页的相当不错的帖子使用您的示例,对于starter,您还应验证page参数是否为整数,并且您可以部分地删除最后一个if-else语句。因此,让我们尝试优化代码:
<?php
$mysqli = new mysqli("localhost", "wanthave", "wanthavepass", "wanthave");
$count_mem = $mysqli->query("SELECT `id` FROM `wanthave_items`");
$rows = $count_mem->num_rows;
$perPage = 5; // items to be shown per page
$num_pages = ceil($rows / $perPage);
$visiblePages = 5; // if current page is 7 show: 5,6,7,8,9,...
$visibleOffset = ceil($visiblePages / 2) - 1; // number of pages to show on the left and on the right of the current page
$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
// Where do you use this ???
// $start = $page == 1 ? 0 : (($page - 1) * $perPage);
if ($num_pages > 1) {
$first = $last = $pagesStr = '';
if($num_pages > $visiblePages){
// no need to show First page if already on it
if($page != 1){
$first = "<a href='index.php?page=1'>First</a>";
}
// no need to show Last page if already on it
if($page < $numPages){
$last = "<a href='index.php?page=$numPages'>Last</a>";
}
// the offset cannot be smaller or bigger than the min & max page number
$fromPage = ($page - $visibleOffset) < 1 ? 1 : ($page - $visibleOffset);
$toPage = ($page + $visibleOffset) > $numPage ? $numPage : ($page + $visibleOffset);
for($i=$fromPage; $i<=$toPage; $i++){
$pagesStr .= "<a href='index.php?page=$i'>$i</a>";
}
echo $first . $pageStr . $last;
} else {
// NOTE: You don't really need this ELSE section at all
// Where is $items defined ???
foreach($items as $item) {
echo "<li>
<a href=\"index.php?page=item-detail&id={$item['id']}\">{$item['name']}</a>
</li>";
}
for ($n = 1; $n < ($num_pages + 1); $n++) {
echo "<a href='index.php?page=$n'>$n</a>";
if ($n < $num_pages) echo ", ";
}
}
}
?>
如果您选择删除ELSE分区,请务必删除
if($num_pages > $visiblePages){
...
}
部分也是如此。这是一项不必要的检查,因为您只需使用$fromPage
和$toPage
来循环所有可用的网页。
我还没有尝试过这段代码,因此可能存在错误,但重要的是不要在代码中的任何位置使用硬编码值,并尝试尽可能多地优化分页过程(并且有这里可以修改一些事情。)