drbfxb977777 2012-10-07 10:34
浏览 97
已采纳

如何使用id按类别排序来保持下一个和之前的分页

My database has 2 tables "category" and "article" ,

Category

id | Category
---------------------
 1   Mobiles
 2   Computers
--------------------
Article


id | Category    | title   |article   
-------------------------------------
 1   2      title      article desc   
 2   1     title      article desc
 3   1     title      article desc
 4   1     title      article desc
 5   2     title      article desc


--------------------------------------

My generated url for articles pages is in this format.

URL/ID/TITLE

I would like to show the next and previous articles titles in the current articles page .

so i used the below code to show the next and previous title with link.

Previous article

$getid = $_GET['id'];
$previous = $getid - 1;
$previousdata = $db->getRow("select * from article WHERE news_id='$previous'");

Next article

$getid = $_GET['id'];
$next = $getid + 1;
$nextdata = $db->getRow("select * from article WHERE news_id='$next'");

And php code like

<a href="http://www.domain.com/article-<?php echo ($previousdata['news_id']); ?>/<?php  echo friendlyURL($previousdata['title']); ?>"> <?php echo  friendlyURL($previousdata['title']); ?></a>

This works fine,It shows the mobiles and computers posts in next and previous links using the id's,

if i post 2 articles in mobiles category the generated id was 12 and 13 for example, and next if i post an article in computer category the generated id was 14. when the user reading mobile article of id 13 ,at bottom it shows previous article title with id 12 and next article title with id of 14 . Obviously the previous article is mobile and next is computers categories.

But i would like to show only the next and previous pagination with "mobiles" category only,

So i try to assign the category

 $getid = $_GET['id'];
 $next = $getid + 1;
 $nextdata = $db->getRow("select * from article WHERE Category=1 AND id='$next'");

This doesn't filter only mobile ..

  • 写回答

2条回答 默认 最新

  • ds000001 2012-10-07 10:46
    关注

    You can directly get it from query as below

    $getid = $_GET['id'];
    $nextdata = $db->getRow("select * from article WHERE Category=1 AND news_id > ".$getid." order by news_id ASC LIMIT 0,1");
    
    $previousdata = $db->getRow("select * from article WHERE Category=1 AND news_id < ".$getid." order by news_id DESC LIMIT 0,1");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。