聪明的PHP分页

时间:2016-04-16 13:22:44

标签: php mysql pagination

我想拆分页面以使其更智能,看起来更好,

我使用此代码进行分页

if (!isset($_GET['page'])){
    $page = 1;
}
else {
    $page = intval($_GET['page']);
}

$numbershownews = 1;
$x = ($page-1)*$numbershownews; 

$select_news_all = $mysqli->query("SELECT id FROM news where FIND_IN_SET('$id_newscats_cat', cats) order by time desc");

$num_news_all = $select_news_all->num_rows;


$cn = $num_news_all/$numbershownews;
$select_news = $mysqli->query("SELECT * FROM news where FIND_IN_SET('$id_newscats_cat', cats) order by time desc limit $x,$numbershownews");
while ($rows_news = $select_news->fetch_array(MYSQL_ASSOC)){

$id_news          = $rows_news ['id'];
$title_news       = $rows_news ['title'];

}

我按此代码显示页面

<?
$pagenext = $page+1;
$pageprev = $page-1;
?>
<ul>
<li>
<?
if (($page == 1) or ($page == "")){
?>
<i class="fa fa-angle-double-right"></i>
<?
}else{
?>
<a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pageprev}.html"; ?>"><i class="fa fa-angle-double-right"></i></a>
<?
}
?>
</li>
<?php
for($i=0; $i<$cn; $i++){
$pagenumber = $i+1;

if($page == $pagenumber){
    echo "<li>{$pagenumber}</li>";
}else{
?>
<li><a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pagenumber}.html"; ?>"><? echo $pagenumber; ?></a></li>

<?
}


}
?>
<li>
<?
if($page >= $cn){
?>
<i class="fa fa-angle-double-left"></i>
<?
}else{
?>
<a class="link" href="<? echo "{$newurltitle_newscats_cat}-c-{$id_newscats_cat}-p-{$pagenext}.html"; ?>"><i class="fa fa-angle-double-left"></i></a>
<?
}
?>

它效果很好,但它给了我这样的页面1,2,3,4,5,6,7,8,9,10我想要像这样分割页面1,2,3,4,...,9,10我该怎么做呢

谢谢

0 个答案:

没有答案