使用PHP排序和分页

时间:2016-06-15 10:34:39

标签: php mysql pdo pagination

我正在尝试使用列标题对记录进行排序,并使用下拉列表显示要显示的记录数。选择时排序和显示记录工作正常。但是,当我选择下面的页码时,记录不会按排序顺序显示它随机选择。

以下是代码

<!doctype html public "-//w3c//dtd html 3.2//en">

<?Php

require "config.php";           // All database details will be included here 
$page_name="demo3.php";
$field='id';
$sort='ASC';
if(isset($_GET['sorting']))
{
  if($_GET['sorting']=='ASC')
  {
  $sort='DESC';
  }
  else
  {
    $sort='ASC';
  }
}
if($_GET['field']=='id')
{
   $field = "id"; 
}
elseif($_GET['field']=='name')
{
   $field = "name";
}
elseif($_GET['field']=='year')
{
   $field="year";
}
elseif($_GET['field']=='rank')
{
   $field="rank";
}   

 //  If you use this code with a different page ( or file ) name then change this 

////// starting of drop down to select number of records per page /////

@$limit=$_GET['limit']; // Read the limit value from query string. 
if(strlen($limit) > 0 and !is_numeric($limit)){
echo "Data Error";
exit;
}

// If there is a selection or value of limit then the list box should show that value , so we have to lock that options //
// Based on the value of limit we will assign selected value to the respective option//
switch($limit)
{
case 1:
$select1="selected";
$select2="";
$select4="";
break;

case 2:
$select2="selected";
$select1="";
$select4="";
break;

default:
$select4="selected";
$select1="";
$select2="";
break;

}

@$start=$_GET['start'];
if(strlen($start) > 0 and !is_numeric($start)){
echo "Data Error";
exit;
}

echo "Select Number of records per page: <form method=get action=$page_name>
<select name=limit>
<option value=1 $select1>1</option>
<option value=2 $select2>2</option>
<option value=4 $select4>4</option>
</select>
<input type=submit value=GO>

";
// You can keep the below line inside the above form, if you want when user selection of number of 
// records per page changes, it should not return to first page. 
// <input type=hidden name=start value=$start>
////////////////////////////////////////////////////////////////////////
//



$eu = ($start - 0); 

if(!$limit > 0 ){ // if limit value is not available then let us use a default value
$limit = 2;    // No of records to be shown per page by default.
 $page=$_GET['p'];
 if($page=='')
 {
  $page=1;
  $start=0;
 }
 else
 {
  $start=$limit*($page-1);
 }

}                             
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 


/////////////// Total number of records in our table. We will use this to break the pages///////
$nume = $dbo->query("select count(*) from search Order by $field $sort")->fetchColumn();
/////// The variable nume above will store the total number of records in the table////


/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";


////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT *  from search Order by $field $sort limit $eu, $limit ";
echo "<TABLE width=50% align=center  cellpadding=0 cellspacing=0> <tr>";
echo'
     <th style=color:blue;><a href="demo3.php?&eu='.$eu.'&limit='.$limit.'&sorting='.$sort.'&field=name">Name</a></th>
<th><a href="demo3.php?eu='.$eu.'&limit='.$limit.'&sorting='.$sort.'&field=year">Year</a></th>
<th><a href="demo3.php?eu='.$eu.'&limit='.$limit.'&sorting='.$sort.'&field=rank">Rank</a></th>
';
//////////////// Now we will display the returned records in side the rows of the table/////////
foreach ($dbo->query($query) as $row) {

if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}

echo "<tr >";
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$row[name]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$row[year]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$row[rank]</font></td>"; 

echo "</tr>";
}
echo "</table>";
////////////////////////////// End of displaying the table with records

/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
if($sort=='ASC')
  {
    $sort='DESC';
  }
  else
  {
    $sort='ASC';
  }
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0) { 
print "<a href='$page_name?start=$back&limit=$limit&sort=$sort'><font face='Verdana' size='2'>PREV</font></a>"; 
} 
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $eu){
echo " <a href='$page_name?start=$i&limit=$limit&sort=$sort'><font face='Verdana' size='2'>$l</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$l</font>";}        /// Current page is not displayed as link and given font color red
$l=$l+1;
}


echo "</td><td  align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume) { 
print "<a href='$page_name?start=$next&limit=$limit&sort=$sort'><font face='Verdana' size='2'>NEXT</font></a>";} 
echo "</td></tr></table>";


?>


</body>

</html>

1 个答案:

答案 0 :(得分:0)

我想我发现了你的问题。在代码的开头,您正在检查名为$ _GET ['sorting']的GET参数,但在您的下一页调用中,GET参数名为“sort”。如果你使它们都具有相同的名称,要么“排序”,要么两者都“排序”,它应该起作用。