2016-06-15 3 views
-1

私は列見出しを使ってレコードをソートしようとしていて、ドロップダウンを使って表示するレコードの数を表示しようとしています。選択したレコードを並べ替えて表示すると、正常に動作します。しかし、私がレコードの下のページ番号を選択するとソートされた順序で表示されませんそれはランダムに選択されて発生します。以下はPHPでソートとページ分割

私は私はあなたの問題を見つけたと思うのコード

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

これは多大なコードです。関連コードを入力して、自分で試したことと何が問題かと思われるものを追加してください。 – Martijn

答えて

0

です。あなたのコードの始めには、$ _GET ['sorting']という名前のGETパラメータをチェックしていますが、次のページ呼び出しではGETパラメータの名前は "sort"です。両方の名前を同じ名前にすると、「並べ替える」または「並べ替える」の両方が機能するはずです。

+0

ありがとうございます。それは働く – user5742277

+0

私は助けることができてうれしいです! – Ingo