价格从高到低就正常,从低到高排序就不正确
price字段是decimal(20,2)类型<select name="picejia" id="picejia">
<option selected="selected">--Default--</option>
<option value="1">high to low</option>
<option value="0">low to high</option>
</select>
<?php
$picejia = $_GET['picejia'];if($picejia==1 and !empty($picejia))
{
$sql="select * from products where order by price desc ";
}
elseif($picejia==0 and !empty($picejia))
{
$sql="select * from products where order by price asc ";
}
elseif(empty($picejia))
{
$sql="select * from products where order by class_id desc";
}
?>
price字段是decimal(20,2)类型<select name="picejia" id="picejia">
<option selected="selected">--Default--</option>
<option value="1">high to low</option>
<option value="0">low to high</option>
</select>
<?php
$picejia = $_GET['picejia'];if($picejia==1 and !empty($picejia))
{
$sql="select * from products where order by price desc ";
}
elseif($picejia==0 and !empty($picejia))
{
$sql="select * from products where order by price asc ";
}
elseif(empty($picejia))
{
$sql="select * from products where order by class_id desc";
}
?>
修改为:select * from products where order by price desc,id desc因为如果你的价格一样的话,排序就出问题了.加多一个标识就好了.
if($picejia==1){
$sql="select * from products where order by price desc ";
}elseif($picejia==0){
$sql="select * from products where order by price asc ";
}else{
$sql="select * from products where order by class_id desc";
}
这样写就好了
我的表结构如下:
create table ceshi(
id int(10) primary key AUTO_INCREMENT,
name varchar(20),
price decimal(20,2)
)engine=myisam,charset=utf8;数据为:排序后由小到大。
id name price
4 ceshi 9.16
2 ceshi 25.44
5 ceshi 29.99
1 ceshi 123.22
3 ceshi 244.25
6 ceshi 444.77
7 ceshi 9444.30
<option selected="selected">--Default--</option>
<option value="1">high to low</option>
<option value="0">low to high</option>
</select>
<?php
$picejia = $_GET['picejia'];if($picejia==1)
{
$sql="select * from products order by price desc ";//个人认为sql写错了,不需要where
}
elseif($picejia==0)
{
$sql="select * from products order by price asc ";//个人认为sql写错了,不需要where
}
else
{
$sql="select * from products order by class_id desc";//个人认为sql写错了,不需要where
}
?>通过了记得加分!