【问题标题】:data not getting displayed in asc order inside foreach loop数据未在 foreach 循环内按 asc 顺序显示
【发布时间】:2015-08-12 05:50:02
【问题描述】:

我有一个存储在$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 循环然后显示数据,我得到了正确的数据,但我无法按升序显示它(acc to 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

【问题讨论】:

  • 提供表定义和一些示例数据集,也使用一个查询而不是循环运行
  • @AK-Sonu 但我需要过滤acc以进行平均
  • @AK-Sonu 我需要按此顺序显示第一个 ID 为 37 的供应商,然后显示 ID 为 26 的供应商,最后显示 ID 为 33 的供应商,我也有更新了我的帖子
  • @roy 您可以使用 php 排序函数对数组进行排序并明智地显示它
  • @Narendra Sisodia 我可以使用升序排序,但我需要根据平均排序

标签: php mysql sql arrays


【解决方案1】:

替换

ORDER BY avgrating DESC

ORDER BY avgrating

【讨论】:

    【解决方案2】:

    试试这个

    <?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 
                        }
                }
        }
    ?>
    

    【讨论】:

    • 仍然没有按升序排列数据
    • 您确定要按平均字段对数据进行排序吗?
    • $result01 代码后的调试结果:echo '
      '; print_r($result01);
    【解决方案3】:

    试试这个代码,它会解决你的问题:

    <?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 
                        }
                }
    
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 2017-01-20
      • 2016-08-10
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      相关资源
      最近更新 更多