Jump to content

Recommended Posts

Hello Everybody,

 

Here is a simple script which am using to fetch results from a database. It works fine but, I need to add paging to display only 10 results per page. Please help me out.

 

//Start Script

<?php

$find = $_POST['find'];

$find1 = mysql_query("SELECT * FROM tableaa WHERE aa_name LIKE '%$find%'");

$matches=mysql_num_rows($find1);

if ($matches == 0 || $find == ""){

echo "No results were found";

}else{

while($result = mysql_fetch_array( $find1 ))

{

$aaId = $result['aa_id1'];$aaaId = $result['aa_id2'];

$prod = getProductDetail($aaId, $aaaId);

extract($prod);

$result = mysql_query("SELECT * FROM table ab WHERE ab_id='$aaaId'");

$row = mysql_fetch_array( $result );

$pId = $row['p_id'];

echo "<a href=index.php?cat=$pId&prod=$aaId>$aa_name</a>, $aa_price<br>$aa_description<br>";

}

}

?>

//End Script

 

Thanks in Advance.

first, id return the number of entries via mysql_num_rows()

 

once you get the number of rows, determine how many results per page you want. (ie, 55 results with 10 results per page would be 6 pages total.  5 with 10 and 1 with 5).  create the links and have them point to the same page, then use the $_GET method to get the page number.  based on the page number, modify the SELECT statement to retrieve only the number of results you want then use the while($var = mysql_fetch_array) statement to display all fields.

 

the $_GET method would be done like...

results.php?page=3

 

then $_GET['page'] would return the page number... 3

change your mysql to include "LIMIT":

$find1 = mysql_query("SELECT * FROM tableaa WHERE aa_name LIKE '%$find%' LIMIT $offset,$rows");

 

from mysql.com (http://dev.mysql.com/doc/refman/5.1/en/select.html)

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1)

 

so for the second page, with 10 pages per page, your variables should be: $offset=11 and $rows=10.

 

regards.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.