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 ..
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");
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
");