2016-06-15 13 views
-1

Ich versuche, die Datensätze mithilfe von Spaltenüberschriften zu sortieren und die Anzahl der anzuzeigenden Datensätze mit einem Dropdown anzuzeigen. Sortieren und Anzeigen der Datensätze, wenn ausgewählt, funktioniert gut. Aber wenn ich die Seitennummern darunter wähle, werden die Datensätze nicht sortiert angezeigt. Es erscheint zufällig ausgewählt.Sortieren und Paginierung mit PHP

Unten ist der Code

<!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> 
+0

Dies ist eine Menge Code. Bitte setzen Sie nur relevanten Code und fügen Sie hinzu, was Sie selbst versucht haben und was Sie denken, das Problem könnte sein, – Martijn

Antwort

0

Ich glaube, ich Ihr Problem gefunden. Am Anfang Ihres Codes überprüfen Sie einen GET-Parameter namens $ _GET ['Sortierung'], aber auf der nächsten Seite rufen Sie den GET-Parameter namens "sort" auf. Wenn Sie beide den gleichen Namen haben, entweder beide "sortieren" oder beide "sortieren" sollte es funktionieren.

+0

Vielen Dank. Es funktioniert – user5742277

+0

Ich bin froh, dass ich helfen konnte! – Ingo