对计算数据进行排序以在php中对数据进行排序
我如何对我的数据进行排序,给出5颗星、4颗星到1颗星的比率 这是我的问题对计算数据进行排序以在php中对数据进行排序,php,mysql,codeigniter,rank,rate,Php,Mysql,Codeigniter,Rank,Rate,我如何对我的数据进行排序,给出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
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
对数据进行排序并给出速率,但不进行回显。
对不起,如果我的问题不清楚,我的英语不好
问候,,
ridho您可以使用
usort
,您可以在此处查看文档:您的示例如下:
$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++;
}
$cost=2000;
$totalbeli=50000;
//我们创建一个空数组。我们需要这个数组,因为PDO结果集是iterable对象,这意味着您不能迭代两次
$my_array=array();
//现在我们将对象放入这个空数组中。结果集中的每一行都将是一个新对象。
foreach($Top5自定义为$data){
//以下是浅表副本(使用关键字“克隆”)。只要字段是基本变量,就可以。
$my_object=克隆$data;
$my\u object->perhitunganrank=($my\u object->freq*$my\u object->totalbeli)-($my\u object->jarak*$cost*$my\u object->freq);
数组推送($my\u数组,$my\u对象);
}
//现在我们需要创建一个函数来比较两个对象,并告诉我们哪一个在这两个对象之间最先出现。
//然后,usort将使用此函数。
//函数使用perhitunganrank属性进行比较。
功能cmp($a$b){
返回strcmp($a->perhitunganrank,$b->perhitunganrank);
}
//现在我们根据上面的函数进行排序
usort($my_数组,“cmp”);
//最后,我们打印现在已排序的对象数组。
foreach($my_数组作为$row_to_print){
$counter=1;
回声“;
回声“$row_to_print->nama_jalan.”否“$row_to_print->no_rumah.”、“$row_to_print->kelurahan.”、“$row_to_print->kecamatan.”、“$row_to_print->kota.”;
echo“$row\u to\u print->jarak.”;
echo“$row_to_print->frekuensi.”;
回显“.$row_to_print->perhitunganrank.”;
回显“.$counter.”;
回声“;
$counter++;
}
您可以使用usort
,您可以在此处查看文档:您的示例如下:
$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++;
}
$cost=2000;
$totalbeli=50000;
//我们创建一个空数组。我们需要这个数组,因为PDO结果集是iterable对象,这意味着您不能迭代两次
$my_array=array();
//现在我们将对象放入这个空数组中。结果集中的每一行都将是一个新对象。
foreach($Top5自定义为$data){
//以下是浅表副本(使用关键字“克隆”)。只要字段是基本变量,就可以。
$my_object=克隆$data;
$my\u object->perhitunganrank=($my\u object->freq*$my\u object->totalbeli)-($my\u object->jarak*$cost*$my\u object->freq);
数组推送($my\u数组,$my\u对象);
}
//现在我们需要创建一个函数来比较两个对象,并告诉我们哪一个在这两个对象之间最先出现。
//然后,usort将使用此函数。
//函数使用perhitunganrank属性进行比较。
功能cmp($a$b){
返回strcmp($a->perhitunganrank,$b->perhitunganrank);
}
//现在我们根据上面的函数进行排序
usort($my_数组,“cmp”);
//最后,我们打印现在已排序的对象数组。
foreach($my_数组作为$row_to_print){
$counter=1;
回声“;
回声“$row_to_print->nama_jalan.”否“$row_to_print->no_rumah.”、“$row_to_print->kelurahan.”、“$row_to_print->kecamatan.”、“$row_to_print->kota.”;
echo“$row\u to\u print->jarak.”;
echo“$row_to_print->frekuensi.”;
回显“.$row_to_print->perhitunganrank.”;
回显“.$counter.”;
回声“;
$counter++;
}
同时发布数据库中的输入和预期输出数据,输出是带有4个冒号的表(alamat、jarak、frekuensi和Rate)。表中的数据按$perhitunganrank Desc sort的结果排序。查询中存在问题。您需要考虑用户只选择年份而不选择月份的情况,反之亦然。否则,查询将是where month='semua'
,它将返回零结果或错误,具体取决于列的数据类型。还将发布数据库中的输入和预期输出数据,输出为4列的表(alamat、jarak、frekuensi和Rate)。表中的数据按$perhitunganrank Desc sort的结果排序。查询中存在问题。您需要考虑用户只选择年份而不选择月份的情况,反之亦然。否则,查询将是where month='semua'
,它将返回零结果或错误,具体取决于您列的数据类型。噢,上帝,非常感谢mastazi先生,代码成功了,它救了我。。我还有一个问题,如果我想再次添加1个冒号(“排名”)包含代表perhitunganrank的数字(例如:perhitunganrank的最大值由数字1表示,第二大值由数字2表示,等等),我希望你能理解我的问题,谢谢..当然!看到我编辑过的答案,我使用了变量$counter,我在手机上,我希望我键入的是正确的:-)哦,上帝,非常感谢mastazi先生代码成功,它救了我。。我还有一个问题,如果我想再次添加1个冒号(“排名”)包含代表perhitunganrank的数字(例如:perhitunganrank的最大值由数字1表示,第二大值由数字2表示,等等),我希望你能理解我的问题,谢谢..当然!请看我编辑的答案,我使用了变量$counter,我正在使用手机,希望键入的是正确的:-)