使用PHP显示“下一步"; “上一个"分页中 [英] Using PHP to show "Next" an "Previous" in pagination

查看:68
本文介绍了使用PHP显示“下一步"; “上一个"分页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码:

   $result = mysql_query("SELECT * FROM table LEFT JOIN table2 
   ON table.field = table2.field WHERE ( table.field = '$pid' ) 
   AND ( table.field5 LIKE '%$q%' OR table.field3 LIKE '%$q%' 
    OR table2.field2 LIKE '%$q%' )");


     if (empty($what)) {

      $countpls = "0";
      } else {

     $countpls = mysql_num_rows($result);
      }


<?php
if ($countpls > 10) {
    echo '<a id=pgnvg href="' . $_SERVER['PHP_SELF'] . '?pg=' . ($startrow + 20) . '&q=' . ($what) . '">Next</a>';
} else {
    echo "";
}

$prev = $startrow - 20;

//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
    echo '<a id=pgnvg2 href="' . $_SERVER['PHP_SELF'] . '?pg=' . $prev . '&q=' . ($what) . '">Previous</a>';
} else {
    echo "";
}
?>

我希望只有在有更多条目要显示时才显示下一个,而只有有更多条目要回圈时才显示前一个.尽管没有更多结果可显示,但它在首页bt上工作,然后在最后一页上工作.接下来显示.

I want the next to show only if there are more entries to show and previous only if there are more entries to circle back to. It works on the first page bt then on the last page Next shows despite teh fact that there are no more results to show.

我尝试添加'else',但仍然无法正常工作.

I tried adding the 'else' but its still not working.

有什么想法吗?

推荐答案

if($countpls > 0){
    $pg = $_POST['pg']?$_POST['pg']:1;

    //if it's not the first page...
    if($pg>1){
        echo '<a id="pgnvg" href="'.$_SERVER['PHP_SELF'].'?pg='.($pg-1).'&q='.$what.'">Previous</a>';
    }
    //if you have more registers to show...
    if(($countpls-(($pg-1)*10))>10){
     echo '<a id="pgnvg" href="'.$_SERVER['PHP_SELF'].'?pg='.($pg+1).'&q='.$what.'">Next</a>';
    }
}

为了计算您要在查询中使用的偏移量,请使用以下方法:

In order to calculate your offset to use in queries, use this:

$offset = ($_POST['pg']-1)*10;

这篇关于使用PHP显示“下一步"; “上一个"分页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆