【问题标题】:sort calculation data to rank my data in php对计算数据进行排序以在 php 中对我的数据进行排名
【发布时间】:2016-05-25 04:24:24
【问题描述】:

如何对我的数据进行排序以给出 5 星、4 星、直到 1 星的评分。

这是我的查询

function top5cust($tahun, $bulan)
{
    if ($tahun == 'semua' && $bulan == 'semua'){
        $data = $this->db->query("SELECT nama_jalan, no_rumah, jarak, kelurahan, kecamatan, kota, COUNT(*) as freq 
                                from t_cust GROUP by nama_jalan, no_rumah, jarak,kelurahan, kecamatan, kota 
                                ORDER by freq DESC LIMIT 5");

    }
    else
    {

        $data = $this->db->query("SELECT nama_jalan, no_rumah, jarak, kelurahan, kecamatan, kota, COUNT(*) as freq 
                                from t_cust
                                WHERE YEAR(tgl_req) = $tahun AND MONTH(tgl_req) = $bulan
                                group by nama_jalan, no_rumah, jarak, jarak,kelurahan, kecamatan, kota 
                                ORDER by freq DESC LIMIT 5");
    }           
    return $data->result();     
}

这是我的 php 代码

<table>
    <thead>
    <tr>
    <th><center>Alamat</center></th>
    <th><center>Jarak</center></th>
    <th><center>Frekuensi</center></th>

    <th><center>Rate</center></th>
    </tr>
    </thead>
    <tbody>
    <?php

    $cost=2000;
    $totalbeli=50000;

    foreach($top5cust as $data){
        $frekuensi=$data->freq;
        $jaraks=$data->jarak;
        $perhitunganrank=($frekuensi*$totalbeli)-($jaraks*$cost*$frekuensi);


        echo "<tr>";
        echo "<td>".$data->nama_jalan." no ".$data->no_rumah.", ".$data->kelurahan.", ".$data->kecamatan.", ".$data->kota."   </td>";
        echo "<td>".$data->jarak."</td>";
        echo "<td>".$data->freq."</td>";
        echo '<td>'.$perhitunganrank.'</td>';
        echo"</tr>";
    }?>
    </tbody>
</table>

如何通过$perhitunganrank 对数据进行排序并给出速率但不回显它。 对不起,如果我的问题不清楚我的英语不好。

问候, 呸呸呸

【问题讨论】:

  • 同时发布您的输入和预期输出
  • 从数据库输入数据,输出为4列(alamat, jarak, frekuensi, and Rate)的表格。表中的数据按 $perhitunganrank Desc 排序的结果排序..
  • 您的查询有问题。您需要考虑用户只选择年份而不选择月份的情况,反之亦然。否则查询将是 where month='semua',这将返回零结果或错误,具体取决于列的数据类型。

标签: php mysql codeigniter rank rate


【解决方案1】:

您可以使用usort,您可以在此处查看文档:http://php.net/manual/en/function.usort.php 您的示例如下:

$cost=2000;
$totalbeli=50000;

//We create an empty array. We need this array because PDO resultsets are iterable objects which means you can't iterate twice

$my_array = array();

//Now we put objects into this empty array. Each row from the resultset will be a new object.

foreach($top5cust as $data){
// The following is a shallow copy (using the keyword 'clone'). It is OK as long as the fields are primitive variables.
    $my_object = clone $data;
    $my_object->perhitunganrank=($my_object->freq*$my_object->totalbeli)-($my_object->jarak*$cost*$my_object->freq);
    array_push($my_array, $my_object);
}

// Now we need to create a function that compares two objects and tells us which one comes first between the two. 
// This function will then be used by usort.
// The function compares using the property perhitunganrank. 

function cmp($a, $b){
    return strcmp($a->perhitunganrank, $b->perhitunganrank);
}

// now we sort based on the function above
usort($my_array, "cmp");

//Finally we print the array of objects which is now sorted.
foreach($my_array as $row_to_print){
    $counter =1;
    echo "<tr>";
    echo "<td>".$row_to_print->nama_jalan." no ".$row_to_print->no_rumah.", ".$row_to_print->kelurahan.", ".$row_to_print->kecamatan.", ".$row_to_print->kota."   </td>";
    echo "<td>".$row_to_print->jarak."</td>";
    echo "<td>".$row_to_print->frekuensi."</td>";
    echo '<td>'.$row_to_print->perhitunganrank.'</td>';
    echo '<td>'.$counter.'</td>';
    echo"</tr>";
    $counter++;
}

【讨论】:

  • 天哪,非常感谢先生。 mastazi 代码成功,它救了我.. 我还有 1 个问题,如果我想再次添加 1 个 colom(“排名”)包含代表 perhitunganrank 的数字(例如:perhitunganrank 的最大值由数字 1 表示,第二大值由数字 2 等表示)我希望你能理解我的问题,谢谢..
  • 当然!查看我编辑的答案,我使用了变量 $counter,我在手机上希望我输入正确:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多