Jump to content

Asking about paging the searching result


Guest alep

Recommended Posts

I have a problem to do paging to my searching result. I had succesful run the page but the problem is..when i select NEXT PAGE...the result of the searching still same with the first page. can you help me?

<p>Result	:</p>
<p>Displaying records from <?php echo ($startRow_Recordset1 + 1) ?> to <?php echo min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1) ?> of <?php echo $totalRows_Recordset1 ?></p>

<table>
<tr>
<?php
$i = 1; //set $i to 1 outside the loop
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) {
echo '<td><img src="bookCover/'.$row_Recordset1['Image'].'.jpg" width=160 height=190></td>'; //display the records in a <td>
$i = $i + 1; //add 1 to $i
if ($i == 5) { //when you have echoed 3 <td>'s
echo '</tr><tr>'; //echo a new row
$i = 1; //reset $i
} //close the if
}//close the while loop
echo '</tr></table>' //close out the table
?>
<p> 
<table border="0">
 <tr>
   <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
       <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a>
       <?php } // Show if not first page ?></td>
   <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
       <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>
       <?php } // Show if not first page ?></td>
   <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
       <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a>
       <?php } // Show if not last page ?></td>
   <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
       <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a>
       <?php } // Show if not last page ?></td>
 </tr>
</table>

Link to comment
Share on other sites

Instead of using a counter ($i) in the loop, you can LIMIT the number of results returned from the database straight from the SQL statement.

 

So for example your SQL string is something like:

$sql = 'SELECT * FROM myTable where criteria=$keyword;';

 

You can limit this sql by using the keyword LIMIT

$sql = 'SELECT * FROM myTable where criteria=$keyword LIMIT 0,5;';

 

This will limit the result from record number 0 to record number 5.

 

So when someone clicks on the "NEXT" button all you need to do is have a way to change this LIMIT numbers to:

$sql = 'SELECT * FROM myTable where criteria=$keyword LIMIT 5,10;';

 

Likewise for page 3:

$sql = 'SELECT * FROM myTable where criteria=$keyword LIMIT 10,15;';

 

More detailed structured info on here: http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

Edited by BeeDev
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...