if(!isset($_REQUEST['id'])) {
header("location: index.php");
}
else
{
$id = $_REQUEST['id'];
}
include ("admin-login/function/db_connect.php");
/* ===================== Pagination Code Starts ================== */
$adjacents = 7;
$statement = $db->prepare("SELECT "
. "tbl_post.post_id, tbl_post.post_title, tbl_post.post_description, tbl_post.post_date, tbl_cate.cat_id, tbl_cate.cat_name, tbl_link.post_id, tbl_link.cat_id "
. "FROM "
. "tbl_post, tbl_cate, tbl_link "
. "WHERE "
. "tbl_post.post_id = tbl_link.post_id "
. "and "
. "tbl_cate.cat_id = tbl_link.cat_id "
. "ORDER BY post_date DESC");
$statement->execute();
$total_pages = $statement->rowCount();
$targetpage = $_SERVER['PHP_SELF']; //your file name (the name of this file)
$limit = 2; //how many items to show per page
$page = @$_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0;
$statement = $db->prepare("SELECT "
. "tbl_post.post_id, tbl_post.post_title, tbl_post.post_description, tbl_post.post_date, tbl_cate.cat_id, tbl_cate.cat_name, tbl_link.post_id, tbl_link.cat_id "
. "FROM "
. "tbl_post, tbl_cate, tbl_link "
. "WHERE "
. "tbl_post.post_id = tbl_link.post_id "
. "and "
. "tbl_cate.cat_id = tbl_link.cat_id "
. "ORDER BY post_date DESC LIMIT $start, $limit");
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
else
$pagination.= "<span class=\"disabled\">« previous</span>";
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
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?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?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?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?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?page=$counter\">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
else
$pagination.= "<span class=\"disabled\">next »</span>";
$pagination.= "</div>\n";
}
/* ===================== Pagination Code Ends ================== */
foreach($result as $row)
{
if ($row['cat_id']==45){ //this is category id. restricted for 45 number category
<div class="loop_page">
<a href="single.php?id=<?php echo $row['post_id']; ?>"> <h1><?php echo $row['post_title']; ?> </h1></a>
}
}
echo $pagination;
如果点击第二页,则转到index.php。请帮助我一位专家。
答案 0 :(得分:0)
您的脚本的第一个条件是,如果未设置$_REQUEST['id']
,它将重定向到您的主页。如果不阅读其余的代码,你应该排除它;你是否通过页面请求传递了一个id?
答案 1 :(得分:0)
我建议你可以使用tabsorter jquery库 http://tablesorter.com/docs/
请在此处查看演示 http://tablesorter.com/docs/example-pager.html
它比编码php简单得多
答案 2 :(得分:0)
你永远不会在你的分页中设置一个id值,并使用$ _GET或$ _POST而不是$ _REQUEST。
答案 3 :(得分:0)
首先这整个代码是可注射的,请使用准备好的查询。
if(!isset($_REQUEST['id'])) {
header("location: index.php");
}
else
{
$id = $_REQUEST['id'];
}
不要重定向设置$ id = 0/1 您使用页面发送请求但缓存只有id。试试以下
if(!isset($_REQUEST['page'])) {
header("location: index.php");
}
else
{
$id = $_REQUEST['page '];
}
**您可以尝试https://github.com/saaiful/datatableHelper。它具有自定义查询构建器和排序,搜索和分页。