Go to next and previous record



Hi guys,

I'm doing php search page. I got the result page and it worked fine. I have set 10 record to be previewed on a page. Could someone tell me how to navigate to next and previous recordset i.e. record 1 to 10, 11 to 20 and so on
This is my code:
$resultID = mysql_query("SELECT nama, id, org, phone, email FROM maintable WHERE nama LIKE '%$nama%' AND org LIKE '%$org%' AND keyword LIKE '%$keyword%' AND bcc LIKE '%$bcc%' ORDER by nama ASC LIMIT 10", $linkID);

Thanks in advance
limit has 2 expressions: limit num1,num2

num1 = starting record
num2 = records after num1

$resultID = mysql_query("SELECT * FROM maintable WHERE nama LIKE '%$nama%' ORDER by nama ASC LIMIT 11,10", $linkID);

will show all records from 11 to 21 (11 + 10). So if you want to show your records by 10 on each page you have to do the code like that:

$resultID = mysql_query("SELECT * FROM maintable WHERE nama LIKE '%$nama%' ORDER by nama ASC LIMIT $start,9", $linkID);

where $start should be 1....11....21....31 (if I remember right) etc. But remember before you do that you should first check how many records you have so you can calculate number of links leading to pages with the next ten records.

eg: if you have 34 records you want 4 pages presenting 10 .. 10 .. 10 and 4 records respectively.
Maybe this helps...
here goes:


$result = mysql_query("SELECT * FROM table");

$show = 10; // Define the number of showed pages
$total = mysql_num_rows($result); // return total rows
$pages = ceil($total / $show); // return total pages

if(!isset($page)) { $page = 0; } /* if there´s no action for a page ($page = 0) then show the fisrt page*/

$begin = $page * $show;
$result = mysql_query("SELECT * FROM table LIMIT $begin, $show");

// -- your news script, guestbook, whatever --------

while($sql = mysql_fetch_array($result)) {
  echo " Put your code here! ";

//---- link generator ---------

if($page> 0) {
  $less = $page - 1;
  $url = "$PHP_SELF?page=$less";
  echo "<a href="$url">previous</a>"; /* link to the previous page*/
for($i=0;$i<$pages;$i++) { /* loop generator with link to the pages*/
  $url = "$PHP_SELF?page=$i";
  echo " | <a href="$url">$i</a>";
if($page< $pages) {
  $more = $page + 1;
  $url = "$PHP_SELF?page=$more";
  echo " | <a href="$url">Next</a>"; /* link to the next page*/


Sorry for my english :p
Thanks for your concern,
Could you please explain to me what $PHP_SELF means?

I was confused because when I click 'Previous' or 'Next' button, it brought me to: http://localhost/php/php.exe/researchers/result.php?page=0
And the problem is my page became slightly slower to load and the image was not completely loaded.

I was wondering where did "php/php.exe" come?

Actually my directory is:

As far as I concern C:\php\php.exe is folder where I put my php.exe file.

Any advise would be appreciated
$PHP_SELF means what page POST or GETS no self.
for example:
<A HREF="<?=$PHP_SELF;?>"Link</A>
I would use
I think that the other is deprecated.
If you want, I can post the function I use for my Previous, page number and next links.
function MultiPage($allrows, $numrows, $limit=10, $offset=0)
	$allrows = Number of rows received by full Data query using mysql_num_rows
	$numrows = Number of rows received by this pages dataquery
	$limit = Max number of rows you want displaed on page
	$offset = To show what row to start query too
	Author: Joel Pinkham
			echo "<p align=\"right\"> \n";
			if ($offset>0) // bypass PREV link if offset is 0
				@print "<a href=\"$PHP_SELF?search=$search&from=$prevoffset&limit=$limit\"><< Previous</a> & \n";
			//Calculate number of pages
			//If there is a remainder another page is added on
			if ($allrows%$limit)
			if ($allrows>$limit)
				$pagenum = intval(($offset/$limit)+1);
				for ($i=1;$i<=$pages;$i++) { //Loop to print page numbers
					if ($i==$pagenum)
						print "$i   \n";
						@print "<a href=\"$PHP_SELF?search=$search&from=$newoffset&limit=$limit\">$i</a> & \n";
			if (($offset+$numrows)<$allrows) //Is this supposed to be the last page?
				@print "<a href=\"$PHP_SELF?search=$search&from=$newoffset&limit=$limit\">Next >></a>\n";
			echo "</p> \n";

It is quite dodgily documented as i just did it for myself. If you have any questions about any part of it (especially the $numrows and $allrows parts) just post back up.