Jump to content

Pagination issue when sorting a table by a specific value ? ie: price, make..


Solarpitch

Recommended Posts

Hi,

 

I am currently using pagination code to allow user's to navigate through a resultset, simple enough. The problem I'm having is.. I'm trying to make the pagination work when a user chooses to sort the table by price, make, model etc..

 

I have a drop down box where the user can select..

 

Sort By: Price, Make, Model

 

This works fine but when the user clicks Next>> to display the next page of results, the pagination code doesnt hold the value that the user just select to sort the table by. Everytime next is clicked, the results format back to default - Hope this makes sense.

 

I just put the pagination code below in case it helps.

 

 

 

<SELECT style ='width:200px;' onchange='window.location.href=this.options[this.selectedIndex].value'>
                            <OPTION VALUE=''>(Select)</OPTION>
                            <OPTION VALUE='http://drivers.php?sortby=make'>make</OPTION>
                            <OPTION VALUE='http://drivers.php?sortby=model'>model</OPTION>
                            <OPTION VALUE='http://drivers.php?sortby=price'>proce</OPTION>
                       
                          </SELECT>

 

 


$sortby = $_GET['sortby'];
		  
		  
$mysql_connect = new mysqli("xxxxxx", "xxxxxx", "xxxxxx");
$mysql_connect->select_db('xxxxxx');

if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
} 

$max=3;			

$query_data = mysqli_fetch_row($result);
$numrows = $query_data[0];


$lastpage = ceil($numrows/$max);

$limit = 'LIMIT ' .($pageno - 1) * $max .',' .$max;

$query = "SELECT * FROM ads WHERE category = 'Drivers' AND validation = 0 ORDER BY '$sortby' $limit";
$result = mysqli_query($mysql_connect, $query) or die (mysqli_error($mysql_connect));


if ($pageno == 1) {
   echo " First Prev ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>First</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>Prev</a> ";
} // if



echo " ( Page $pageno of $lastpage ) ";



for($i = 1; $i <= $lastpage; $i++){
        if($i == $pageno){
            echo("[".$i."] ");
        }else{
            echo("<a href=\"$PHP_SELF?pageno=$i\">$i</a> ");
        }
    }



	if(($numrows % $max) != 0){
        if($i == $pageno){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?pageno=$i\">$i</a> ");
        }
    } 



if ($pageno == $lastpage) {
   echo " Next Last ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>Next</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>Last</a> ";
   }

 

 

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.