源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP简单实现上一页下一页功能示例

  • 时间:2021-09-11 12:50 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP简单实现上一页下一页功能示例
本文实例讲述了PHP简单实现上一页下一页功能。分享给大家供大家参考,具体如下: [b]思路整理:[/b] 现在好多人用id的增1和减1实现上一篇和下一篇,但是难道文章ID不会断了吗?所以你要知道上个ID和个ID是多少就OK了。 那怎么解决这个问题呢,很简单! [b]例子:[/b] 假如这篇文章的ID200
<a href="?action=up&id=200">上一篇</a>
<a href="?action=down&id=200">下一篇</a>

如果是实现上一篇就在action=up页面写函数
$id= $_GET['id'];
//上一篇:
$sql= select * from article where id < '.$id.' order by id desc limit 0,1';
$rs= mysql_query($sql);
$row= mysql_fetch_array ($rs);
//下一篇:
$sql= select * from article where id < '.$id.' order by id asc limit 0,1';
$rs= mysql_query($sql);
$row= mysql_fetch_array ($rs);

原理,查询比当前ID小(where id < '.$id.'上一篇)和比当前ID大(where id > '.$id.'下一篇)的1条(limit 0,1)数据,并按降序(desc,上一篇)和升序(asc,下一篇)显示出来,当只取一篇的时候,可以省略降序或升序。 具体实现代码:注需要传递参数 前台在上一篇,下一篇处调用:
<?php
 echo GetPreNext(pre,news,$_REQUEST[catid],$_REQUEST[id]);?>
//显示上一篇下一篇
 function GetPreNext($gtype,$table,$catid,$id){
 $preR=mysql_fetch_array(mysql_query("select * from ".$table." where catid=".$catid." and id<$id order by id desc limit 0,1"));//id比传入id小的最近一条
 $nextR=mysql_fetch_array(mysql_query("select * from ".$table." where catid=".$catid." and id>$id order by id asc limit 0,1"));//id比传入id大的最近一条
  $next = (is_array($nextR) ? " where id={$nextR['id']} " : ' where 1>2 ');
  $pre = (is_array($preR) ? " where id={$preR['id']} " : ' where 1>2 ');
   $query = "Select * from ".$table." ";
      $nextRow =mysql_query($query.$next);
      $preRow = mysql_query($query.$pre);
      if($PreNext=mysql_fetch_array($preRow))
      {
       echo $PreNext['pre'] = "上一篇:<a href='newsshow.php?id=".$preR['id']."&&catid=".$catid."'>".$PreNext['title']."</a> ";
      }
      else
      {
       echo $PreNext['pre'] = "上一篇:没有了 ";
      }
      if($PreNext=mysql_fetch_array($nextRow))
      {
       echo $PreNext['next'] = "下一篇:<a href='newsshow.php?id=".$nextR['id']."&&catid=".$catid."'>".$PreNext['title']."</a> ";
      }
      else
      {
        echo $PreNext['next'] = "下一篇:没有了 ";
      }
}

代码经测试可用 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/150.htm]php排序算法总结[/url]》、《[url=http://www.1sucai.cn/Special/242.htm]PHP常用遍历算法与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/180.htm]php正则表达式用法总结[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》及《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部