我有一个存储在$finalvendorid
中的数组,看起来像这样
Array
(
[0] => Array
(
[id] => 14
[vendorid] => 26
[area] => N1
)
[1] => Array
(
[id] => 13
[vendorid] => 33
[area] => N1
)
[2] => Array
(
[id] => 15
[vendorid] => 37
[area] => N1
)
)
从此我需要获取vendorid并显示其数据。我试图使用foreach循环然后显示数据,我正确地获得了数据,但我无法按升序显示它(根据avgrating)
供应商表的演示视图是
id vendorname vendordesc avgrating
26 V1 VD1 2
33 V2 VD2 3
37 V3 VD3 1
循环代码
<?php
foreach ($finalvendorid as $keyy => $valuee)
{
$newvendorid = $valuee['vendorid'];
$sql01 = "SELECT * FROM vendor where id='".$newvendorid."' ORDER BY avgrating ASC";
//echo $sql01;
$result01 = mysqli_query($con, $sql01);
if (mysqli_num_rows($result01) > 0)
{
while($row01 = mysqli_fetch_assoc($result01))
{
//displaying of data
}
}
}
?>
任何人都可以告诉我如何按照asc顺序排列数据,以便按以下顺序获得o / p供应商
first vendor with id 37 should get displayed,
then vendor with id 26 should get displayed and
finally vendor with id 33 should get displayed
答案 0 :(得分:0)
替换
ORDER BY avgrating DESC
与
ORDER BY avgrating
答案 1 :(得分:0)
试试这个
<?php
foreach ($finalvendorid as $keyy => $valuee)
{
$newvendorid = $valuee['vendorid'];
$sql01 = "SELECT * FROM vendor where id='".$newvendorid."' ORDER BY avgrating DESC";
echo $sql01;
$result01 = mysqli_query($con, $sql01);
if (mysqli_num_rows($result01) > 0)
{
while($row01 = mysqli_fetch_assoc($result01))
{
//displaying of data
}
}
}
?>
答案 2 :(得分:0)
尝试使用此代码可以解决您的问题:
<?php
$ids=array(); $v_ids=null;
/*get vendore id and store in ids array*/
foreach($finalvendorid as $key => $value){
$ids[] = $value['vendorid'];
}
if(count($ids) > 0){
/* make string of all vendore id seperated by comma */
$v_id = implode(",",$ids);
/*start your query here with order by clause */
$sql01 = "SELECT * FROM vendor where id IN($v_id) ORDER BY avgrating ASC";
//echo $sql01;
$result01 = mysqli_query($con, $sql01);
if (mysqli_num_rows($result01) > 0)
{
while($row01 = mysqli_fetch_assoc($result01))
{
//displaying of data
}
}
}
?>