Php 对HTML表中的数据进行排序
希望你能帮忙 我有一个数据库,我从中查询到一个php文件,并对其进行进一步处理 我需要一种在表中按$distance对结果进行排序的方法(最低优先)-但由于这不是数据库的一部分,我无法将orderby构建到查询中 $distance由php函数计算,表示两个位置之间的距离 /**/Php 对HTML表中的数据进行排序,php,html,sorting,html-table,Php,Html,Sorting,Html Table,希望你能帮忙 我有一个数据库,我从中查询到一个php文件,并对其进行进一步处理 我需要一种在表中按$distance对结果进行排序的方法(最低优先)-但由于这不是数据库的一部分,我无法将orderby构建到查询中 $distance由php函数计算,表示两个位置之间的距离 /**/ echo” 名称 地点一 距离 要求交付给 地点二 "; 如果($distance
echo”
名称
地点一
距离
要求交付给
地点二
";
如果($distance<5)//如果小于5英里
{
回声“;
回显“$row['name']”;
回显“$行['locationOne]”;
回声“.”约“$distance.”英里“;
回显“$row['locationTwo']”;
回声“;
}
/**/我建议您在回显表格之前进行排序。PHP有很多方法可以根据元素的值对数组进行排序。这是关于这些功能的官方文档。最简单的方法如下:
function sortByOrder($a, $b) {
return $a['order'] - $b['order'];
}
usort($myArray,'sortByOrder')编码>计算完距离后再循环一次
$rows_within_distance = array();
foreach ($rows as &$row) {
// calc $distance
$row['distance'] = $distance;
if ($distance < 5) $rows_within_distance = $row;
}
usort($rows_within_distance, function($a, $b) {
return $a['distance'] - $b['distance'];
});
foreach ($rows_within_distance as $row) {
// output; echo <tr>....</tr>
}
$rows_in_distance=array();
foreach($rows as&$row){
//计算距离
$row['distance']=$distance;
如果($distance<5)$rows\u in_distance=$row;
}
usort($rows_)在距离内,函数($a,$b){
返回$a['distance']-$b['distance'];
});
foreach($rows\u距离内的行数为$row){
//输出;回音。。。。
}
尝试以下代码:
// Your db result assumed
$db_results = array(
array(
'name' => 'name1',
'locationOne' => 'loc1a',
'locationTwo' => 'loc1b',
),
array(
'name' => 'name2',
'locationOne' => 'loc2a',
'locationTwo' => 'loc2b',
),
array(
'name' => 'name3',
'locationOne' => 'loc3a',
'locationTwo' => 'loc3b',
),
);
// Calculated the distance and stored in another array
$result = array();
$i = 0;
foreach($db_results as $res)
{
$result[$i] = $res;
// calculating distance and adding to the array
$result[$i]['distance'] = your_function($res['locationOne'], $res['locationTwo']);
$i++;
}
// Function for Multi-dimensional Sort Array by Value
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC)
{
$sort_col = array();
foreach ($arr as $key=> $row)
{
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
// Sorted the array
array_sort_by_column($result, 'distance');
// Printed the final table
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Location One</th>
<th>Distance</th>
<th>Require Delivery To</th>
<th>Location Two</th>
</tr>";
foreach($result as $res)
{
if($res['distance'] < 5)
{
echo "<tr>";
echo "<td>" . $res['name']."</td>";
echo "<td>" . $res['locationOne'] . "</td>";
echo "<td>" ."approx ". $res['distance']. " miles" ."</td>";
echo "<td>" . $res['locationTwo']."</td>";
echo "</tr>";
}
}
echo "</table>";
//假设您的数据库结果
$db_results=数组(
排列(
'name'=>'name1',
'位置一'=>'位置一',
'locationTwo'=>'Location1B',
),
排列(
“名称”=>“名称2”,
'locationOne'=>'Location2A',
'locationTwo'=>'loc2b',
),
排列(
'name'=>'name3',
'locationOne'=>'Location3A',
'locationTwo'=>'loc3b',
),
);
//计算距离并存储在另一个数组中
$result=array();
$i=0;
foreach($db_结果为$res)
{
$result[$i]=$res;
//计算距离并添加到阵列
$result[$i]['distance']=您的_函数($res['locationOne'],$res['locationTwo']);
$i++;
}
//用于按值对数组进行多维排序的函数
函数数组按列排序(&$arr、$col、$dir=sort\u ASC)
{
$sort_col=array();
foreach($arr作为$key=>$row)
{
$sort_col[$key]=$row[$col];
}
数组多排序($sort\u col、$dir、$arr);
}
//对数组进行排序
数组按列排序($result,'distance');
//打印最终表格
回声“
名称
地点一
距离
要求交付给
地点二
";
foreach(结果为$res)
{
如果($res['distance']<5)
{
回声“;
回显“$res['name']”;
echo“$res['locationOne]”;
回声“.”约“$res['distance']”英里“;
回显“$res['locationTwo']”;
回声“;
}
}
回声“;
您好,&$row是什么?是打字错误还是什么我还不知道的!!变量是通过引用分配的,因此如果在循环中修改$row
,$rows[i]
实际上会被修改。在de foreach docs中了解更多信息:
// Your db result assumed
$db_results = array(
array(
'name' => 'name1',
'locationOne' => 'loc1a',
'locationTwo' => 'loc1b',
),
array(
'name' => 'name2',
'locationOne' => 'loc2a',
'locationTwo' => 'loc2b',
),
array(
'name' => 'name3',
'locationOne' => 'loc3a',
'locationTwo' => 'loc3b',
),
);
// Calculated the distance and stored in another array
$result = array();
$i = 0;
foreach($db_results as $res)
{
$result[$i] = $res;
// calculating distance and adding to the array
$result[$i]['distance'] = your_function($res['locationOne'], $res['locationTwo']);
$i++;
}
// Function for Multi-dimensional Sort Array by Value
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC)
{
$sort_col = array();
foreach ($arr as $key=> $row)
{
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
// Sorted the array
array_sort_by_column($result, 'distance');
// Printed the final table
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Location One</th>
<th>Distance</th>
<th>Require Delivery To</th>
<th>Location Two</th>
</tr>";
foreach($result as $res)
{
if($res['distance'] < 5)
{
echo "<tr>";
echo "<td>" . $res['name']."</td>";
echo "<td>" . $res['locationOne'] . "</td>";
echo "<td>" ."approx ". $res['distance']. " miles" ."</td>";
echo "<td>" . $res['locationTwo']."</td>";
echo "</tr>";
}
}
echo "</table>";