Jump to content

PHP/MySQL Result Paging Problem


NickG21

Recommended Posts

Hey everyone,

I'm looking to put the results of my MySQL query into multiple pages. I followed the online tutorial at http://www.evolt.org/node/19340 in order to get as far as I am. With the code below I display the first page, counts the proper amount of results, creates the proper links on the bottom of the page such as Page 1 | Page 2 etc... depending on the number of results. The only thing that doesn't work is displaying these next pages. When i click on these links or the "Next" link for the next page it just reloads the same first 9 results. If you have any idea what to do i'd appreciate it

thank you

 

<?php if(!empty($_GET['searchterm'])){
	$search = mysql_real_escape_string(trim($_GET['searchterm']));		

if (!($limit)){
     $limit = 9;} // Default results per-page.

if ( !$page or $page < 0 ) { $page = 0; }  // Default page value.

$numresults = mysql_query("SELECT * FROM tblProduct WHERE PrName LIKE '%". $search ."%'"); // the query.

$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.

if ($numrows == 0){
     	echo("No results found matching your query - $search"); // bah, modify the "Not Found" error for your needs.
     exit();}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows % $limit) {
$pages++;} // has remainder so add one page

$current = intval($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.

else{
$last = $numrows;} // If last results page, last result equals total number of results.

if(isset($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $pagecount){
$page = (int)$_GET['page'];	
}
$skip = ($page - 1) * $resultsper;

?>
	</div><span class="sectionheader">Search Results:</span><span class="items"><?php echo writeShoppingcart(); ?></span>
		<div id="content">
			<table width="100%" border="0">
				 <tr>
  						<td width="50%" align="left">
						Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  						</td>
  						<td width="50%" align="right">
						Page <b><?=$current?></b> of <b><?=$total?></b>
  						</td>
					</tr>
				</table>

<?php

// Now we can display results.
$result = mysql_query("SELECT * FROM tblProduct WHERE PrName LIKE '%". $search ."%' ORDER BY PrName ASC LIMIT $page, $limit");

echo '<div id="parentdiv" style="width:660px">';
while($row = mysql_fetch_array($result)){
      echo '<div style="width:220px;float:left;">';
      echo '<div class="itemname"> <a href="products.php?id=' . $row['PrID'] . '"> ' . $row['PrDispName'] . '</a></div>';
      echo '<div class="brand">Maker: ' . $row['PrBrand'] . '</div>';
  echo '<div class="recentproduct"><img src="' . $row['PrImage'] . '" alt=' . $row['PrName'] . '"></div>';
      echo '<div class="price">$ ' . $row['PrPrice'] . '</div>';
      echo '<div class="addtocart"><a href="cart2.php?action=add_item&id=' . $row['PrID'] . '&qty=1">Add to Cart</a></div>';
        
echo '</div>';
?>
<?
}?>
</div>
<?php
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?searchterm=$search&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
?>

<table>
	<tr>
		<td align="left" width="50%">
<?php	
echo("<b>$i</b>\n");} // If current page don't give link, just text.
else{ 
echo("<a href=\"$PHP_SELF?searchterm=$search&page=$ppage&limit=$limit\">$i</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?searchterm=$search&page=$next_page&limit=$limit\">next</a>");}
?>
	</td>
	<td width="50%" align="right">

Results per-page: <a href="<?=$PHP_SELF?>?searchterm=<?=$search?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?searchterm=<?=$search?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?searchterm=<?=$search?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?searchterm=<?=$search?>&page=<?=$page?>&limit=50">50</a>
	</td>
</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/147066-phpmysql-result-paging-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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