网上邻居 2010-01-13

php读取mysql排序问题

<?php
require_once("conn.php");

?>

<html>

<body>

<?php
$exec_idasc="select * from news order by id asc;
$result=mysql_query($exec_idasc);

while ($rs=mysql_fetch_array($result)){ //循环读取出数据
?>
<tr>
<td><?php echo $rs['id'];?></td>
<td><?php echo $rs['title'];?></td>
<td><?php echo $rs['addtime'];?></td>
</tr>

<?php } ?>
</table>
</body>
</html>

目前已经可以按照id 升序排列mysql库内容了,我想通过下拉菜单方式选择排列方式,但不知道应该如何添加代码,请指点,谢

<form action="list" method="post" name="listform" id="listform">
<select name="select" size="1">
<option selected>id 升序</option>
<option>id 降序</option>
<option>title 升序</option>
<option>title 降序</option>
<option>time 升序</option>
<option>time 降序</option>
</select>
</form>

满意答案

热心问友 2011-01-06

<?php
require_once("conn.php");

?>

<html>

<body>

<?php
$str_zd=$_GET['zd'];
$str_type=$_GET['type'];
if (isset($str_zd))
{
$sql_px=$str_zd;
}else
{
$sql_px="id";//默认id
}

if (isset($str_type))
{
$sql_type=$str_type;
}else
{
$sql_type="asc";//默认排序
}
$exec_idasc="select * from news order by ".$sql_px.$sql_type;
$result=mysql_query($exec_idasc);

while ($rs=mysql_fetch_array($result)){ //循环读取出数据
?>
<tr>
<td><?php echo $rs['id'];?></td>
<td><?php echo $rs['title'];?></td>
<td><?php echo $rs['addtime'];?></td>
</tr>

<?php } ?>
</table>
<form action="list" method="post" name="listform" id="listform">
<select name="select" size="1">
<option value="?zd=id&type=asc" selected>id 升序</option>
<option value="?zd=id&type=desc">id 降序</option>
<option value="?zd=title&type=asc">title 升序</option>
<option value="?zd=title&type=desc">title 降序</option>
<option value="?zd=time&type=asc">time 升序</option>
<option value="?zd=time&type=desc">time 降序</option>
</select>
</form>
</body>
</html>

/////////////////////////////////////////////////

也不知道能不能帮助你。下班了。如果有问题。在聊

满意答案

ㄒo┌;莞鎂 9级 2010-01-13

<select name="select" size="1" onchange="location.href=this.value">
<option selected value="目标页面?order=id&fs=asc">id 升序</option>//order传递排序关键字,fs传递排序方式
<option value="目标脚本?order=id&fs=desc">>id 降序</option>
<option>title 升序</option>
<option>title 降序</option>
<option>time 升序</option>
<option>time 降序</option>
</select>

然后你在目标页面取得传递的那两个参数,然后sql语句这样写

$exec_idasc="select * from news order by $order $fs";

追问:

觉得你这种方式更合理一些,但按照你的方式填写后提示

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\wammp\htdocs\code\list.php on line 47

具体内容:

$exec="select * from news order by $order $fs";

$result=mysql_query($exec); //执行上面的SQL语句,并把结果赋值给$result变量
while ($rs=mysql_fetch_array($result)){ //循环读取出数据
?>

回答:
$order $fs这两个变量你取值了没呀?
你要用这样的方式先取值$order=$_GET['order'];$fs=$_GET['fs'];
然后再sql
提示的那错误就是说sql执行失败
追问:
牛x,可以显示了,请问默认以id 升序显示应该如何写?
回答:
这也很简单,我给你完整的代码吧
if(isset($order=$_GET['order'])){
$order=$order=$_GET['order'];
}else{
$order="id";//默认为id排序
}
if(isset($fs=$_GET['fs'])){
$fs=$fs=$_GET['fs'];
}else{
$fs="";//默认为asc,因为asc可以省略,所以可以留空
}
追问:

首行增加了一条 <option>请选择排序方式</option> ,搞定!

若干选项都搞定,不过默认只打开list.php页面没有选择排序条件会出现错误代码,如何实现不选择条件则不显示记录

回答:
if(isset($order=$_GET['order'])){//如果有参数传递过来
$order=$order=$_GET['order'];
//要执行的数据库操作
}
补充:

呃,老大,我代码多打了几个,你可以改下嘛

$order=$order=$_GET['order'];

改成

$order=$_GET['order'];

其他回答(2)

PHPER 初级团 合作回答者:1人 2010-01-13

<SELECT id=select1 LANGUAGE=javascript onchange="location.href=this.value">
<OPTION value=1.html>1</OPTION>
<OPTION value=2.html>2</OPTION></SELECT>

网上邻居 2级 2010-01-13

$order=$_GET['order'];
$fs=$_GET['fs'];
if(isset($order)){
$order=$_GET['order'];
}else{
$order="id";//默认为id排序
}
if(isset($fs)){
$fs=$_GET['fs'];
}else{
$fs="asc";//默认为asc,因为asc可以省略,所以可以留空
}

修整了下