Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Prev/Next ID issue

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Prev/Next ID issue

Postby inoob » Fri Sep 20, 2013 1:09 pm

Code: Select all
<?php
$_GET['id']=$id;
$currentid = $id;
$prevquery= "SELECT * FROM database WHERE fldID < $currentid ORDER BY fldID DESC LIMIT 1";
$prevresult = mysqli_query($conn,$prevquery) or die(mysqli_error());
while($prevrow = mysqli_fetch_array($prevresult))
{
$previd = $prevrow['fldID'];
}
$nextquery= "SELECT * FROM database WHERE fldID > $currentid ORDER BY fldID ASC LIMIT 1";
$nextresult = mysqli_query($conn,$nextquery) or die(mysqli_error());
while($nextrow = mysqli_fetch_array($nextresult))
{
$nextid  = $nextrow['fldID'];
}
?>
<b>
<a href="article.php?id=<?php echo $previd;?>">Previous</a>
<a href="article.php?id=<?php echo $nextid;?>">Next</font></a>



I noticed when the Next button reaches to the last record, it doesn't return back to the first record of the database, rather it shows an error at the end that it cant find the last ID and it just ruins the page. This same goes when I click Previous button till the last record, it shows an error. Is there any way to return the script back to the first record after it has reached the last record in the database? Any help would be appreciated.
inoob
New php-forum User
New php-forum User
 
Posts: 31
Joined: Tue Nov 06, 2012 12:20 pm

Re: Prev/Next ID issue

Postby johnj » Sun Sep 22, 2013 9:11 pm

Once you reach the maximum number of available records( which is the last page), you should not increase the counter or fetch more records. Whenever you click next, make sure you are not incrementing the page number beyond the last page.

The same idea can be used for "Previous" page.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Prev/Next ID issue

Postby Strider64 » Tue Sep 24, 2013 2:55 am

Code: Select all
if ($currentid > $lastrecord) {
    $currentid = $startid // First id in Database
} elseif ($currentid < $startid) {
    $currentid = $lastid // User is visiting previous button
    /* You could have $currentid = $startid if you don't want the user to cycle through */
}
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 62
Joined: Sat Mar 23, 2013 8:24 am

Re: Prev/Next ID issue

Postby inoob » Wed Sep 25, 2013 12:06 pm

Strider64 wrote:
Code: Select all
if ($currentid > $lastrecord) {
    $currentid = $startid // First id in Database
} elseif ($currentid < $startid) {
    $currentid = $lastid // User is visiting previous button
    /* You could have $currentid = $startid if you don't want the user to cycle through */
}


Thank makes a lot of sense. Thats a good one. Thanks.
inoob
New php-forum User
New php-forum User
 
Posts: 31
Joined: Tue Nov 06, 2012 12:20 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.