Jump to content

next and previous


dachshund

Recommended Posts

hi,

 

i have a website which contains features, interviews, reviews and a blog. all the articles pretty much run down in a blog format, but you can also select a specific section like 'interviews' in the nav bar.

 

i would like to put next and previous buttons at the bottom of each article, which would take you to the next of that type.

 

for example, if you're on an interview, next would take you to the next interview (which would be the next lower id where section == 'interview' in this case) and never a feature or review etc.

 

in the address bar i have s=interview (or whatever the section is).

 

so i can do WHERE `section` like $s, for the next button, but what can i add to this to find the next (lower or higher) id?

 

i hope that all makes sense.

 

thanks for any help in advance.

Link to comment
https://forums.phpfreaks.com/topic/191772-next-and-previous/
Share on other sites

there is stuff like LIMIT start_from, num_of_rows_to_take in MySQL

 

Example:

SELECT * FROM topics LIMIT 0, 20;

 

will return first 20 entires from table

 

SELECT * FROM topics LIMIT 80, 20;

returns 20 entries from table starting from 80th

 

so if you would pass some $_GET var via url lets say $_GET['page']

 

setup your sql like $sql = "SELECT * FROM topics LIMIT ".($_GET['page'] * 20).", 20 ORDER BY date DESC"; //will take 20 entries from table and sort them from newest to oldest

Link to comment
https://forums.phpfreaks.com/topic/191772-next-and-previous/#findComment-1010772
Share on other sites

then maybe u can do it like so

 

lets say u have url which leads tu exact article number similar to

article.php?id=10

//connection
$AID = $_GET['id'];

if(is_numeric($AID) && $AID > 0)
{
//now the query
if($_GET['step'] == 'next')
{
$sql = "SELECT * FROM articles WHERE id>$AID ORDER BY id ASC LIMIT 1";
}
else if($_GET['step'] == 'prev')
{
$sql = "SELECT * FROM articles WHERE id<$AID ORDER BY id DESC LIMIT 1";
}
else
{
//if non of the above select exact article
$sql = "SELECT * FROM articles WHERE id=$AID LIMIT 1";
}

//execute query
$result = mysql_query($sql);

//your ouput here

//finaly <<prev next>> buttons
echo "<a href='article.php?id=$AID&step=prev'>prev</a>";
echo "<a href='article.php?id=$AID&step=next'>next</a>";
}

 

theoreticly it shoud work

try out and say if it's working

Link to comment
https://forums.phpfreaks.com/topic/191772-next-and-previous/#findComment-1010809
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.