2016-06-15 4 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

관련 문제