在HTML表格中排序数据


Sorting data in HTML table

希望你能帮忙,

我有一个数据库,我从查询到一个php文件,并做一些进一步的处理。

我需要一种按$distance(最低优先)对表中的结果排序的方法-但由于这不是数据库的一部分,我不能在查询中构建ORDER by。

$distance由php函数计算,表示两个英国地点之间的距离

/* <----代码计算两点之间的距离--------> */

 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>";
if($distance < 5) // if less than 5 miles
  {
    echo "<tr>";
    echo "<td>" . $row['name']."</td>";
    echo "<td>" . $row['locationOne'] . "</td>";
    echo "<td>" ."approx ". $distance. " miles" ."</td>";               
    echo "<td>" . $row['locationTwo']."</td>";
    echo "</tr>";
 }  

/* <——需要一种方法来显示这些结果,首先在上面的表头中按最低距离排序——> */

我建议您在回显表之前进行排序。PHP有很多方法可以根据元素的值对数组进行排序。这里是有关这些函数的官方文档php.net/array.sorting。最简单的是这样:

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

尝试以下代码:

// 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>";