php按字段排序
field.php
<?php
header("content-type:text/html;charset=utf-8");
$cn=mysql_connect("localhost","root","1*****u");
$db=mysql_select_db("test");
$field=$_REQUEST["field"];
if($field==""){
$field="id";
}
else{
$field=$_REQUEST["field"];
}
$order=$_REQUEST["order"];
if($order==""){
$order="asc";
}
else{
$order=$_REQUEST["order"];
}
//echo $field;
$sql="select * from emotion order by ".$field." ".$order;
//echo $sql;
mysql_query("set names utf8");
$query=mysql_query($sql);
$num=mysql_num_rows($query);
$pageSize=10; //每页显示的记录数
if(isset($_GET['page'])) //获取当前页数
{
$page=intval($_GET['page']);
}
else
{
$page=1;
}
if($num)
{
if($num<$pageSize)
{
$pageNum=1; //只有一页
}
if($num % $pageSize)
{
$pageNum=(int)($num/$pageSize)+1;
}
else
{
$pageNum=$num/$pageSize;
}
}
else
{
$pageNum=0;
}
////////////////////////////
$pageString = ''
if( $page == 1 ){
$pageString .= '第一页 | 上一页| '
}
else{
$pageString .= '<a href=?page=1&field='.$_REQUEST["field"].'&order='.$_REQUEST['order'].'>首 页</a> | <a href=?page='.($page-1).'&field='.$_REQUEST["field"].'&order='.$_REQUEST['order'].'>上一页</a>| '
}
if( ($page == $pageNum) || ($pageNum == 0) ){
$pageString .= ' 下一页 | 尾页'
}
else{
$pageString .= '<a href=?page='.($page+1).'&field='.$_REQUEST["field"].'&order='.$_REQUEST["order"].'>下一页</a> | <a href=?page='.$pageNum.'&field='.$_REQUEST["field"].'&order='.$_REQUEST['order'].'>尾页</a>'
}
$fristNum=($page-1)*$pageSize;
$sql="select * from emotion order by ".$field." ".$order." limit $fristNum,$pageSize";
//echo $sql;
$query=mysql_query($sql);
echo "<table style="font-size:12px;line-height:24px;font-family:arial;" border=1 align="center">";
echo "<Tr>";
if($order=="asc"){
echo "<td width=100>"."<A href=field.php?field=id&order=desc>编号</a>"."</td>";
}
else{
echo "<td width=100>"."<A href=field.php?field=id&order=asc>编号</a>"."</td>";
}
if($order=="asc"){
echo "<td>"."<A href=field.php?field=content&order=desc>内容</a>"."</td>";
}
else{
echo "<td>"."<A href=field.php?field=content&order=asc>内容</a>"."</td>";
}
if($order=="asc"){
echo "<td width=131>"."<A href=field.php?field=datetime&order=desc>日期</a>"."</td>";
}
else{
echo "<td width=131>"."<A href=field.php?field=datetime&order=asc>日期</a>"."</td>";
}
echo "</tr>";
while($row=mysql_fetch_array($query)){
echo "<tr>";
echo "<td>".$row["ID"]."</td>";
echo "<td>".$row["content"]."</td>";
echo "<td>".$row["datetime"]."</td>";
echo "</tr>";
}
echo "<tr><td colspan=3 align=center>".$pageString." 共". $pageNum."页|当前第".$page."页,共有".$num."条记录</td></tr>";
echo "</table>";
?>