I have been doings bits here and there on some code which essentially pulls data down from mysql and displays through php. Results are displayed 3 at a time on the page and I have never had any issues.
I have recently added a field to the database items however: ‘sold out’ and ‘in stock’ and am trying (miserably) to get two searches going – firstly I want to display the items in stock, and then those that have sold out. Unfortunately, the pagination code I have used for years doesn’t like me running two php queries and is simply adding 3 extra items to the page (where applicable).
Full code is:
<form name="form1" method="get" action="products.php">
<?php
if(!empty($msg)) {
echo $msg[0];
}
?>
<input name="q" class="textInput2" input type="search" id="q" placeholder="search image name..." autosave="applestyle_srch" results="5" onKeyUp="applesearch.onChange('srch_fld','srch_clear')" />
<input name="doSearch" type="hidden" id="doSearch2" value="Search">
<?php if ($get['doSearch'] == 'Search') {
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM products";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 3;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
if($get['q'] == '') {
$sql = "SELECT * FROM products WHERE status='sold' ORDER BY `price` ASC LIMIT $offset, $rowsperpage";
}
else {
$sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage";
}
$result1 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
if($get['q'] == '') {
$sql = "SELECT * FROM products WHERE status='in stock' ORDER BY `price` ASC LIMIT $offset, $rowsperpage";
}
else {
$sql = "select * from products where `title` like '%$_REQUEST[q]%' LIMIT $offset, $rowsperpage";
}
$result2 = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
?></form></div>
<div id="pagination" style="float:right; display:inline; margin-right:10px;">
page:<?php
/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$prevpage'><</a> ";
} // end if
// range of num links to show
$range = 3;
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?q=&doSearch=Search¤tpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>
</div>
</div>
</div>
</div></p>
<div class="category-products">
<form name "searchform" action="products.php" method="post">
<?php while($rrows = mysql_fetch_array($result2))
{
echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">';
echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /></a></div>';
echo '<div id="searchdetails">
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
<td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>';
echo '</td>
</tr>
<tr>
<td>' . $rrows['desc'] . '';
echo '</td>
</tr>
<tr>
<td><h3 class="price">£' . $rrows['price'] . ' ' . $rrows['pandp'] . '</h3>'; echo '</td>
</tr>
</table></div>';
echo '</div>';
}
?>
<?php while($rrows = mysql_fetch_array($result1))
{
echo '<div><div id="searchimage"><a class="product-image" href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">';
echo '<img src="' . $rrows['photo'] . '" width="225" height="150" alt="" title="' . $rrows['title'] . '" /><img src="soldout.png" id="soldout" /></a></div>';
echo '<div id="searchdetails">
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>
<td height="30"><h3 class="product-name"><a href="productspec.php?productcode=' . $rrows['productcode'] . '" title="' . $rrows['title'] . '">' . $rrows['title'] . '</a></h3>';
echo '</td>
</tr>
<tr>
<td>' . $rrows['desc'] . '';
echo '</td>
</tr>
<tr>
<td><h3 class="price">£' . $rrows['price'] . ' ' . $rrows['pandp'] . '</h3>'; echo '</td>
</tr>
</table></div>';
echo '</div>';
}
?>
</form> <?php } ?>
I can fully appreciate that the code could be cleaner – (I haven’t revised the pagination process for years now as it has always worked) – but am assuming there must be an easier way to display all the items in the table witthout having to run both the $result1 and $result2 queries.
Any help much appreciated!!
JD
Here’s a function I made for a board I’ve programmed. I hope this helps you in some way.
It is a bit messy (to me) since I haven’t updated in awhile.
The 5 required variables this function asks for is the number of rows, the url of your page so that links work well, the “posts per page” which basically means how many results you want per page (default 20), the name of the link so if you want the next page to appear at the page link, and finally if you want to change what $_GET you want to change the page to. Default is $_GET[‘page’].
Now you will need to change the code a bit to suit it to your needs but next you will need to place this in your query. Here’s some example use:
The important thing is to use $p[querylimit] to limit your query and use $pages for your pagination links. Hope this is useful!
Edit: I have also recently created another pagination function you may want instead of the above as this one uses a select option list rather than a bunch of links. And I also fixed a bug I noticed an hour ago. Funny how I caught it as I was going over this again.