我在数据库中取出结果集的形式使这种:id name res price
1 联想电脑1 私塾 4500
1 联想电脑2 私塾 4000
1 联想电脑3 私塾 4600
1 联想电脑4 私塾 4200
1 联想电脑5 私塾 4300
1 联想电脑6 私塾 4400
这样取出来后如何根据price升序排列呢?我 select * from _goods order by price asc 这样根本就排列不到.我的price是varchar型的.各位帮帮我啊...我把结果集存到array里排序
$data[]=$row['price'];
sort($data,SORT_NUMERIC);
print_r($data);
结果是:
Array ( [0] => 15000 )
Array ( [0] => 5700 [1] => 15000 )
Array ( [0] => 4500 [1] => 5700 [2] => 15000 )
Array ( [0] => 4500 [1] => 5100 [2] => 5700 [3] => 15000 )
Array ( [0] => 4000 [1] => 4500 [2] => 5100 [3] => 5700 [4] => 15000 )各位高手指点哈如何根据price来升序排列.先谢过了.....我太菜了.....
1 联想电脑1 私塾 4500
1 联想电脑2 私塾 4000
1 联想电脑3 私塾 4600
1 联想电脑4 私塾 4200
1 联想电脑5 私塾 4300
1 联想电脑6 私塾 4400
这样取出来后如何根据price升序排列呢?我 select * from _goods order by price asc 这样根本就排列不到.我的price是varchar型的.各位帮帮我啊...我把结果集存到array里排序
$data[]=$row['price'];
sort($data,SORT_NUMERIC);
print_r($data);
结果是:
Array ( [0] => 15000 )
Array ( [0] => 5700 [1] => 15000 )
Array ( [0] => 4500 [1] => 5700 [2] => 15000 )
Array ( [0] => 4500 [1] => 5100 [2] => 5700 [3] => 15000 )
Array ( [0] => 4000 [1] => 4500 [2] => 5100 [3] => 5700 [4] => 15000 )各位高手指点哈如何根据price来升序排列.先谢过了.....我太菜了.....
$data[]=$row['price'];
sort($data,SORT_NUMERIC);
print_r($data);
print_r(array_reverse($data));
$sql="select * from _itemtypeinfo,_suppliersinfo,_purchasegoodsinfo,_goodspriceinfo where
_suppliersinfo.tid=_itemtypeinfo.id and _purchasegoodsinfo.sid=_suppliersinfo.id and
_purchasegoodsinfo.tid=_itemtypeinfo.id and _goodspriceinfo.gid=_purchasegoodsinfo.id
and _itemtypeinfo.state!=1 and _goodspriceinfo.state!=1 and _purchasegoodsinfo.state!=1 and _suppliersinfo.state!=1 and _itemtypeinfo.typename like '%".$content."%' or
_suppliersinfo.supplyname like '%".$content."%' or _purchasegoodsinfo.goodsname like '%".$content."%' ";
}
$result=mysql_query($sql) or die("SQL Error: <br>".$sql."<br>".mysql_error());
$count=mysql_num_rows($result);
if($count!=0){/*function ORD_ASC($a,$b){
if($a==$b){
return 0;
}
return ($a<$b)?-1:1;
}
*/
while($row=mysql_fetch_array($result)){
$data[]=$row['price'];
//$data1[]=$row['id'];
//$array=array("id"=>$data1,"price"=>$data);
//usort($data,"ORD_ASC");
sort($data,SORT_NUMERIC);
print_r($data);
//echo sizeof($array['price']);
//echo $data[0];
//print_r($data);
/* foreach($data as $price)*/
/*$count=count($data);*/
for($i=0;$i<count($data[0]);$i++){}
?>
<tr>
<td><input type="checkbox" name="select[]" value="" /></td>
<td><?=$row['typename']?></td>
<td><?=$row['goodsname']?></td>
<td><?=$row['supplyname']?></td>
<td><? echo $data[$i]; ?></td>
<td><?=$row['tel1']."/".$row['tel2']."/".$row['tel3']."/".$row['tel4'] ?></td>
</tr>
<?
}
/* }
}
}
}*/
}
else{
?>
<tr>
<td colspan="6"><font color="blue">很抱歉,沒有相關的資料.</font></td>
</tr>
<?
}
?>
楼上兄台给我的那个运行结果是:
Array ( [0] => 15000 )
Array ( [0] => 5700 [1] => 15000 )
Array ( [0] => 4500 [1] => 5700 [2] => 15000 )
Array ( [0] => 4500 [1] => 5100 [2] => 5700 [3] => 15000 )
Array ( [0] => 4000 [1] => 4500 [2] => 5100 [3] => 5700 [4] => 15000 ) 我向的是在把价格遍历出来写到表格里去,而且是按照价格升序排列的....谢谢了......
$old = Array ( 4000 ,4500 , 5100 , 5700 , 15000, 3,500, 2000 ) ;
for($i=0;$i<count($old);$i++)
for($j=0;$j<count($old)-1-$i;$j++)
if($old[$j]<$old[($j+1)])
list($old[$j],$old[($j+1)]) = array($old[($j+1)], $old[$j]);
print_r($old);//大到小$old = Array ( 4000 ,4500 , 5100 , 5700 , 15000, 3,500, 2000 ) ;
for($i=0;$i<count($old);$i++)
for($j=0;$j<count($old)-1-$i;$j++)
if($old[$j]>$old[($j+1)])
list($old[$j],$old[($j+1)]) = array($old[($j+1)], $old[$j]);
print_r($old);//小到大
1 联想电脑2 私塾 4000
1 联想电脑4 私塾 4200
1 联想电脑5 私塾 4300
1 联想电脑6 私塾 4400
1 联想电脑1 私塾 4500
1 联想电脑3 私塾 4600
2.array_multisort(手册一下)
$result=mysql_query($sql);while($row=mysql_fetch_row($result)){ //把每行的结果放入数组,这里共有4个1维数组
$i++; //出循环后,$i=记录集的总行数
$id[$i]=$row[0];
$name[$i]=$row[1];
$res[$i]=$row[2];
$price[$i]=$row[3];
}for($j=1;$j<$i;$j++){ //按$price从小到大输出
for($k=$j+1;$k<$i+1;$k++){
if($price{[$j]>$price[$k]){
$a=$id{[$j]; $id{[$j]=$id[$k];$id[$k]=$a;
$a=$name{[$j]; $name{[$j]=$name[$k];$name[$k]=$a;
$a=$res{[$j]; $res{[$j]=$res[$k];$res[$k]=$a;
$a=$price{[$j]; $price{[$j]=$price[$k];$price[$k]=$a;
}
}
echo $id[$j].'.'.$name[$j].'.'.$res[$j].'.'.$price[$j].'<br>';
}
select * from _goods order by price+0 asc
试试