我正在使用http://www.phpeasystep.com/phptu/29.html进行分页,之前在以前的网站上使用过它,效果很好。
我现在在不同的网站上使用它,其代码布局与之前网站上的pdo语句不同。
分页工作正常,第1页显示1,2,3,4,5链接,当我点击第2页或更多时,结果显示,但分页链接消失。
如果我在网址中添加& page = 2或3或4,则会显示正确的结果,但不会显示分页链接。
如果我手动将$ total_pages设置为一个数字,例如22,那么分页链接将适用于每个页面,所以我认为问题是在第1页之后没有设置$ total_pages。
我正在使用的PDO声明是:
$tbl_name="index";
$adjacents = 3;
$targetpage = "search.php";
$limit = 6;
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
if ($_GET['search'] == '' && $_GET['cat'] == 'acategory') {
$cat = $_GET['cat'];
$search = '';
$array = $pdo->prepare("SELECT * from `index` where `category_name` = 'acategory' LIMIT :start, :limit");
$array->execute(array(':start' => $start, ':limit' => $limit));
$query = $pdo->prepare("SELECT COUNT(*) from `index` where `category_name` = 'acategory' LIMIT :start, :limit");
$query->execute(array(':start' => $start, ':limit' => $limit));
$total_pages = $query->fetchColumn();
}
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
if ($page > 1)
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$prev\">� previous</a>";
else
$pagination.= "<span class=\"disabled\">� previous</span>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$lastpage\">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$lastpage\">$lastpage</a>";
}
else
{
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$counter\">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?search=$search&cat=$cat&page=$next\">next �</a>";
else
$pagination.= "<span class=\"disabled\">next �</span>";
$pagination.= "</div>\n";
}
<?=$pagination; echo $total_pages; ?>
有一些if语句,但我知道正确的if语句是由我看到的结果执行的。
我试图在第1页之后回显$ total_pages变量并且它没有输出任何内容,并且它没有显示任何错误。
为什么$ total_pages会被设置在第一个页面上,而不是后面的页面,当所有更改的页面是$ page和$ start变量?
由于
答案 0 :(得分:1)
你有两个问题。
如果您正在运行PHP 5&gt; = 5.1.0,请关闭模拟准备,这将解决您的LIMIT问题。请参阅此Answer
$pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
分页问题是由LIMITING查询引起的。
TRY
$query = $pdo->prepare("SELECT COUNT(*) from `index` where `category_name` = 'acategory' ");
$query->execute();
$total_pages = $query->fetchColumn();