Jump to content

Archived

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

FreakOn

Problem mixing Pagination with search results

Recommended Posts

Hi All, I wish to thank anybody willing to help with my problem.

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 '&nbsp;Just Echoing to fault find<br />';
echo '&nbsp;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />&nbsp;';
echo $str_SQL.'<br />&nbsp;';
*/
$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"><<&nbsp;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&nbsp;>></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>&nbsp;</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>&nbsp;&nbsp;('.$total_results.' product'.$thes.' found - showing product '.$fromhowmany.' to  '.$tohowmany.')<br /><br />

<form name="maxresults" method="post" action="search.'.$fext.'">
'.$maxresultsf.'
&nbsp;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>&nbsp;</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]

Share this post


Link to post
Share on other sites
Hey FreakOn,

Honesly, I don't want to read through your code. :D  Here is a pagination class that will allow you to specify parameters and easily paginate without hassle:

[code=php:0]
class Pagination {
    public $currentPage, $itemCount, $itemsPerPage, $linksHref, $linksToDisplay;
    public $pageJumpBack, $pageJumpNext, $pageSeparator;
    public $queryString, $queryStringVar;
/*
void (int current page)
*/
    public function setCurrentPage($reqCurrentPage){
        $this->currentPage = (integer) abs($reqCurrentPage);
    } // end setCurrentPage
/*
void setItemCount(int item count)
*/
    public function setItemCount($reqItemCount){
        $this->itemCount = (integer) abs($reqItemCount);
    } // end setItemCount
/*
void setItemsPerPage(int items per page)
*/
    public function setItemsPerPage($reqItemsPerPage){
        $this->itemsPerPage = (integer) abs($reqItemsPerPage);
    } // end setItemsPerPage
/*
void setLinksHref(string hyperlink)
*/
    public function setLinksHref($reqLinksHref){
        $this->linksHref = $reqLinksHref;
    } // end setLinksHref
/*
void setLinksFormat(string "Go Back", string seperator, string "Next")
*/
    public function setLinksFormat($reqPageJumpBack, $reqPageSeparator, $reqPageJumpNext){
        $this->pageJumpBack = $reqPageJumpBack;
        $this->pageSeparator = $reqPageSeparator;
        $this->pageJumpNext = $reqPageJumpNext;
    } // end setLinksFormat
/*
void setLinksToDisplay(int links to display)
*/
    public function setLinksToDisplay($reqLinksToDisplay){
        $this->linksToDisplay  = (integer) abs($reqLinksToDisplay);
    }  // end setLinksToDisplay
/*
void setQuerySTringVar(string query string variable)
*/
    public function setQueryStringVar($reqQueryStringVar){
        $this->queryStringVar = $reqQueryStringVar;
    } // end setQueryStringVar
/*
void setQueryString(string query string)
*/
    public function setQueryString($reqQueryString){
        $this->queryString = $reqQueryString;
    } // end setQueryString
    /*
array getCurrentCollection(array collection)
STILL IN DEVELOPMENT!!!
*/
    public function getCurrentCollection($reqCollection){
        if($this->currentPage < 1){
            $start = 0;
        } elseif($this->currentPage > $this->getPageCount()){
            $start = $this->getPageCount() * $this->itemsPerPage - $this->itemsPerPage;
        } else {
            $start = $this->currentPage * $this->itemsPerPage - $this->itemsPerPage;
        } // end else
        return array_slice($reqCollection, $start, $this->itemsPerPage);
    } // end getCurrentCollection
/*
int getPageCount(void)
*/
    public function getPageCount(){
        return (integer)ceil($this->itemCount/$this->itemsPerPage);
    } // end getPageCount
/*
string getPageLinks(void)
*/
    public function getPageLinks(){
        $strLinks = '';
        $pageCount = $this->getPageCount();
        $queryString = $this->getQueryString();
        $linksPad = floor($this->linksToDisplay/2);
if($this->linksToDisplay == -1){
$this->linksToDisplay = $pageCount;
} // end if
        if($pageCount == 0){
            $strLinks = $this->pageJumpBack.' 1 '.$this->pageJumpNext;
        } elseif($this->currentPage - 1 <= $linksPad || ($pageCount - $this->linksToDisplay + 1 == 0)) {
            $start = 1;
        } elseif($pageCount - $this->currentPage <= $linksPad) {
            $start = $pageCount - $this->linksToDisplay + 1;
        } else {
            $start = $this->currentPage - $linksPad;
        } // end else
        if(isset($start)) {
            if($start > 1) {
                if(!empty($this->pageJumpBack)) {
                    $pageNum = $start - $this->linksToDisplay + $linksPad;
                    if($pageNum < 1){
                        $pageNum = 1;
                    } // end if

                    $strLinks .= '<a href="'.$this->linksHref.$queryString.$pageNum.'">';
                    $strLinks .= $this->pageJumpBack.'</a>'.$this->pageSeparator;
                } // end if
                $strLinks .= '<a href="'.$this->linksHref.$queryString.'1">1...</a>'.$this->pageSeparator;
            } // end if
            if($start + $this->linksToDisplay > $pageCount) {
                $end = $pageCount;
            } else {
                $end = $start + $this->linksToDisplay - 1;
            } // end else
            for($i = $start; $i <= $end; $i ++){
                if($i != $this->currentPage){
                    $strLinks .= '<a href="'.$this->linksHref.$queryString.($i).'">';
                    $strLinks .= ($i).'</a>'.$this->pageSeparator;
                } else {
                    $strLinks .= $i.$this->pageSeparator;
                } // end else
            } // end for loop
            $strLinks = substr($strLinks, 0, -strlen($this->pageSeparator));
            if($start + $this->linksToDisplay - 1 < $pageCount) {
                $strLinks .= $this->pageSeparator.'<a href="'.$this->linksHref.$queryString.$pageCount.'">';
                $strLinks .= '...'.$pageCount.'</a>'.$this->pageSeparator;
                if(!empty($this->pageJumpNext)) {
                    $pageNum = $start + $this->linksToDisplay + $linksPad;
                    if($pageNum > $pageCount){
                        $pageNum = $pageCount;
                    } // end if
                    $strLinks .= '<a href="'.$this->linksHref.$queryString.$pageNum.'">';
                    $strLinks .= $this->pageJumpNext.'</a>';
                }  // end if
            } // end if
        } // end if
        return $strLinks;
    } // end getPageLinks
/*
string getQueryString(void)
*/
    public function getQueryString(){
        $pattern = array('/'.$this->queryStringVar.'=[^&]*&?/', '/&$/');
        $replace = array('', '');
        $queryString = preg_replace($pattern, $replace, $this->queryString);
        if(!empty($queryString)) {
            $queryString.= '&amp;';
        } // end if
        return '?'.$queryString.$this->queryStringVar.'=';
    } // end if
    public function getSqlLimit() {
        return ' LIMIT '.($this->currentPage * $this->itemsPerPage - $this->itemsPerPage).', '.$this->itemsPerPage;
    } // end getSqlLimit
/*
void __construct(void)
*/
    public function __construct(){
        $this->setCurrentPage(1);
        $this->setItemsPerPage(20);
        $this->setItemCount(0);
        $this->setLinksFormat('&laquo; Back',' &bull; ','Next &raquo;');
        $this->setLinksHref($_SERVER['PHP_SELF']);
        $this->setLinksToDisplay(5);
        $this->setQueryStringVar('page');
        $this->setQueryString($_SERVER['QUERY_STRING']);
        if(isset($_GET[$this->queryStringVar]) && is_numeric($_GET[$this->queryStringVar])){
            $this->setCurrentPage($_GET[$this->queryStringVar]);
        } // end if
    } // end construct
} // end class
[/code]

-- Will http://www.phpfever.com/

Share this post


Link to post
Share on other sites

×

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.