Forum Moderators: coopster

Message Too Old, No Replies

next article in list

array of articles from database

         

abushahin

7:38 pm on Sep 12, 2011 (gmt 0)

10+ Year Member



hi guys
i have articles that are generated dynamically from the db by sending varibles via GET. so page.php?id=2 returns an article, what i want to do is have the next article in line at the bottom something like read next article or even the title of the article would be great. I can do that by adding 1 to the current id but that would return a page not found when the articles run out. any ideas, i havent found much on the www those that i have found are about pagination which isnt what i really want or is it?
any help appreciated.

penders

8:14 pm on Sep 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are the article IDs always continuous, no gaps?

I think you should run a SQL query to get the next id greater than the current one. That same query could return the article title and whatever other information you might want?

Readie

8:52 pm on Sep 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe...

$sql = 'SELECT * FROM table_name WHERE id > ' . $current_id . ' ORDER BY id ASC LIMIT 1';
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
$next_link = ($row)? '<a href="/page.php?id=' . $row->id . '">Next</a>' : '';

<?php echo $next_link; ?>


--

Heh, I more than skim read Pender's post this time.

abushahin

1:25 pm on Sep 15, 2011 (gmt 0)

10+ Year Member



thanks for the reply, but the problem with this is that when it reaches the end so page.php?id=10 lets say the last one is 10, there will still be 11 and no content, how do i resolve that?

penders

5:20 pm on Sep 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It shouldn't be a problem. That's one reason for querying the database. The SQL SELECT will fail if there is no record with an id greater than the current id. Readie's code already handles this...

$next_link = ($row)? '<a href="/page.php?id=' . $row->id . '">Next</a>' : '';


$next_link will be an empty string when you are on the last record.

abushahin

6:25 pm on Sep 16, 2011 (gmt 0)

10+ Year Member



Vielen Dank!
Merci beaucoup!
&#38750;&#24120;&#24863;&#35874;&#24744;
Thank you so much!