Re: << Previous / Next Buttons >>

From: Date: Thu, 06 Sep 2001 17:47:50 +0000
Subject: Re: << Previous / Next Buttons >>
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-66250@lists.php.net to get a copy of this message
I just use LIMIT its alot better, alot less CPU intensive. if (!isset($pos)) $pos = 0 SELECT * FROM product LIMIT 0, $pos $pos++ if ( !((SELECT count(*) FROM product) > $pos) ) <a href='$PHP_SELF?pos=$pos'>Next</a> you get the idea. Chris Lee lee@mediawaveonline.com "Jeff Oien" <jeff@webdesigns1.com> wrote in message news:PGEBKOPJJHHICHGDKCCICEEEDDAA.jeff@webdesigns1.com... > > Hi, what's the simplest way to provide previous/next buttons that navigate > > through individual records of a database? This is for a photo gallery, so I > > want to do one record at a time until I'm out of records, then I want the > > "next" button to disappear. I've been fiddling with it for a while now, but > > haven't been very successful. Anyone have any snippets, or ideas for me? > > > > Thanks in advance. > > I'll show you what I did but realize I'm an advanced beginner/intermediate > programmer. This may not be the most efficient way to do it but seems to > be the simplest code compared to other ways of doing it that I've seen. Not > sure if I will explain this well enough either. And this is for a fairly small > database. With a larger one it may be too inefficient. > > First count how many records are in the database for the album and put > it in $count. > > Then loop through all the records in the album, place the id for each record > in an array and find out where in the array the current record is. $photo_id > is fed to this script which tells it which picture to display: > > $x = 0; > while ($row = mysql_fetch_array($result)) { > $photo_id1 = $row['photoid']; > $array[$x] = $photo_id1; > if ($photo_id1 == $photo_id) { > $position = $x; > } > $x++; > } > > In another sql query for retrieving the specific image (have to compensate for > array[] starting at zero but record count starting at 1): > > $row = mysql_fetch_array($result); > $photo_id = $row['photoid']; > if ($position == 0) { > $photo_pos = $position + 1; > echo "<p>Showing $photo_pos of $count &lt;&lt; Previous"; //no link > } > else { > $photo_pos = $position + 1; > $previous_pos = $photo_pos - 2; > $previous = $array[$previous_pos]; > echo "<p>Showing $photo_pos of $count <a > href=\"display.php?photo_id=$previous\">&lt;&lt; Previous</a>"; > } > if ($count == $photo_pos) { > echo " | Next &gt;&gt;"; //no link > } > elseif ($photo_pos < $count) { > $next_pos = $photo_pos; > $next = $array[$next_pos]; > echo " | <a > href=\"display.php?photo_id=$next\">Next &gt;&gt;</a>"; > } > > Comments from more experienced programmers welcome. I don't want to lead > astray. > Jeff Oien

« previous php.general (#66250) next »