2

我的数据库有 2 个表 "category" 和 "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


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

我为文章页面生成的 url 就是这种格式。

URL/ID/TITLE

我想在当前文章页面中显示下一篇和上一篇文章的标题。

所以我使用下面的代码来显示下一个和上一个标题的链接。

上一篇文章

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

下一篇

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

和 php 代码之类的

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

这很好用,它使用 id 显示下一个和上一个链接中的手机和计算机帖子,

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 ..

4

2 回答 2

1

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");
于 2012-10-07T10:46:04.330 回答
1

If I understand you want to get then next/previous article id in the same category:

The next article:

$nextdata = $db->getRow("
    select * 
    from article 
    where Category = 1 and id > $current_article_id
    order by id
    limit 1
");

The previous article:

$previousdata = $db->getRow("
    select * 
    from article 
    where Category = 1 and id < $current_article_id
    order by id desc
    limit 1
");
于 2012-10-07T10:47:20.023 回答