FreakOn
-
Posts
1 -
Joined
-
Last visited
Never
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.
Problem mixing Pagination with search results
in PHP Coding Help
Posted
I have been scratching my head for days now and wish to put this to bed so maybe I can join it...!
The Problem
++++++++++
Connecting to database and collecting results is not a problem, but attempting to paginate the results and then disply the returned results (product info) is a problem. Some searches can return upto 2000 results, with most returning +200 to 700 so the pages need paginating.
So far the pagination appears to be working OK, but now I cannot get the product info to display on the page. $content is the display to the page.
Any Help much appreciated.
My Code
+++++++
[code]
<?php
/*#################################
Image Pop-Up Loader
#################################*/
$content='<script type="text/javascript" src="assets/javascripts/image.js"></script>
';
/*######################################
Connect to the Database
######################################*/
$db = new dblib;
$db->connect_host(DB_SERVER, DB_USER, DB_PASS);
/*#####################################
Store the Search Choice
in the user session
Dependent on the Method
used -> $_GET or $_POST
#####################################*/
if($_GET){
if($_GET[gender]){
$_SESSION[st]='pr_sex';
$_SESSION[sc]=strtoupper($_GET[gender]);
}
if($_GET[range]){
$_SESSION[st]='pr_type';
$_SESSION[sc]=strtoupper($_GET[range]);
}
}
if($_POST){
if($_POST[st]=="house"){
$_SESSION[st]='pr_house';
$_SESSION[sc]=strtoupper($_POST[house]);
$_SESSION[maxresults]=$_SESSION[maxresults];
}else{
if($_POST[showresults]){
$_SESSION[maxresults]=$_POST[showresults];
$_SESSION[st]=$_SESSION[st];//'pr_house'
$_SESSION[sc]=$_SESSION[sc];
}else{
$_SESSION[st]='pr_house';
$_SESSION[sc]='';
$_SESSION[maxresults]=$_SESSION[showresults];
}
}
}
/*###########################################
First Printed Page Element
###########################################*/
/*##############################
PAGINATION SCRIPT
##############################*/
/*###############################
Set current, prev and next page
###############################*/
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
$prev = ($page - 1);
$next = ($page + 1);
/*####################
Max results per page
####################*/
if(!$_SESSION[maxresults]){
$max_results = 25;
}else{
$max_results = $_SESSION[maxresults];
}
$maxresultsf='
<select name="showresults" onchange=maxresults.submit()>
<option value="">select</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
</select>
';
/*####################
Calculate the offset
####################*/
$from = (($page * $max_results) - $max_results);
/*##################################
POSSIBLE PROBLEM
##################################*/
$str_SQL="SELECT * FROM pr_list WHERE freestock > 0 AND ".$_SESSION[st]."='".$_SESSION[sc]."'";
/*
echo '<br /><br />';
echo ' Just Echoing to fault find<br />';
echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br /> ';
echo $str_SQL.'<br /> ';
*/
$result=$db->query_db("$dbconn",$str_SQL);
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $max_results);
$pagination = '<div id="pagination">';
/*##################################
Create a PREV link if there is one
##################################*/
if($page > 1){
$pagination .= '<a href="?st='.$_GET[st].'&sc='.$_SESSION[sc].'&page='.$prev.'" class="pagination"><< Previous</a>';
}
/*############################
Loop through the total pages
############################*/
if($page > 1){ // Slip a dash - into the display if page number = more than 1
$slipin=' - ';
}
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
$pagination .= ''.$slipin.''.$i.'';
}else{
$pagination .= ' - <a href="?st='.$_GET[st].'&sc='.$_SESSION[sc].'&page='.$i.'" class="pagination">'.$i.'</a>';
}
}
/*###############################
Print NEXT link if there is one
###############################*/
if($page < $total_pages){
$pagination .= ' - <a href="?st='.$_GET[st].'&sc='.$_SESSION[sc].'&page='.$next.'" class="pagination">Next >></a>';
}
/*###########################################################
Now we have our pagination links in a variable($pagination)
ready to print to the page. I put it in a variable because
you may want to show them at the top and bottom of the page
###########################################################*/
/*#####################################
Below is how you query the db for
ONLY the results for the current page
#####################################*/
//$result=mysql_query("select * from pr_list LIMIT $from, $max_results ");
if($_GET[page]){
// REMOVED --> ORDER by pr_type ASC
$result=mysql_query("SELECT * FROM pr_list WHERE ".$_SESSION[st]."='".$_SESSION[sc]."' AND freestock > 0 LIMIT $from, $max_results");
}else{
$result=mysql_query("SELECT * FROM pr_list WHERE ".$_SESSION[st]."='".$_SESSION[sc]."' AND freestock > 0 LIMIT $from, $max_results");
}
if($_GET[sc]){
$thissc=$_GET[sc];
}else{
$thissc=$_SESSION[sc];
}
// Close the Pagination and add a <p> to
// keep firefox seperated from the results
// IE seperates OK without the <p> (bugger)
$pagination .= '</div><p> </p>';
/*#########################
Start returning the results
#########################*/
if(mysql_num_rows($result)>0){
/*##################
ADDED
##################*/
$thisbrand=$rows[pr_brand];
if($total_results > 1){
$thes='s';
}else{
$thes='';
}
if($total_results > $max_results){
$pagination=$pagination;
}else{
$pagination='';
}
$fromhowmany=$from + 1;
$tohowmany=$from + $max_results;
$content.='<strong>'.$_SESSION[sc].' Product Range</strong> ('.$total_results.' product'.$thes.' found - showing product '.$fromhowmany.' to '.$tohowmany.')<br /><br />
<form name="maxresults" method="post" action="search.'.$fext.'">
'.$maxresultsf.'
Number of products per page ('.$max_results.')</form><br />
'.$pagination.'';
// Populate the Fields
$ptname=$rows[pr_desc];
while($i = mysql_fetch_array($result)){
$content.='
<blockquote id="searchreturn">
<div id="imbor">'.$primage.'</div><strong>'.$ptname.' '.$rows[pr_desc2].'</strong>
<blockquote>
<strong>brand:</strong> '.ucwords(strtolower($rows[pr_brand])).'<br /><br />
<strong>category:</strong> '.ucwords(strtolower($rows[pr_type])).' - '.ucwords(strtolower($rows[pr_sex])).'<br /><br />
<strong>price:</strong> £ '.$unitprice.'
<div align="right"><a href="productorder.'.$fext.'?product='.base64_encode($rows[stockcode]).'" title="Order this product"><img src="assets/images/button_order.gif" alt="order" class="imgmiddle" align="right"></a></div>
<p> </p>
</blockquote>
</blockquote>
';
}
$content.='
('.$max_results.' items per page, '.$total_results.' '.$_SESSION[sc].' product'.$thes.' found - showing product '.$fromhowmany.' to '.$tohowmany.')<br /><br />
'.$pagination.'';
}
?>
[/code]