Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中使html表列可排序?_Php_Sorting - Fatal编程技术网

如何在php中使html表列可排序?

如何在php中使html表列可排序?,php,sorting,Php,Sorting,我有一个只使用文本文件填充的html表,它基本上是按时间戳排序的。 我希望还可以选择通过名为Server的$keys列对表数据进行排序,以便根据服务器名称查看排序后的数据。既然这不是使用mysql,我也不能使用“order by”,有没有办法做到这一点?如何在php中实现这一点 下面是我的一些代码,显示了如何创建我的表: $keys = array('Server', 'Target','Set','Time', 'Length','Size','Status'); echo '<tabl

我有一个只使用文本文件填充的html表,它基本上是按时间戳排序的。 我希望还可以选择通过名为Server的$keys列对表数据进行排序,以便根据服务器名称查看排序后的数据。既然这不是使用mysql,我也不能使用“order by”,有没有办法做到这一点?如何在php中实现这一点

下面是我的一些代码,显示了如何创建我的表:

$keys = array('Server', 'Target','Set','Time', 'Length','Size','Status');
echo '<table id="stats_1"><tr>';
foreach ($keys as $column)
   echo '<th>' . $column . '</th>';
    echo '</tr>';

$counter=0;
foreach ($data as $row){
  $counter ++;
    $class = $counter % 2 === 0 ? 'alt1' : 'alt2';
    echo '<tr class="' . $class . '">';
     foreach ($keys as $column){
        if (isset($row[$column])){
          echo '<td>' . $row[$column] . '</td>';
        } elseif ($column == 'Status') {
          echo '<td> Check Logs </td>';
        } elseif ($column == 'Length') {
          echo '<td> n/a </td>';
        } elseif ($column == 'Size') {
          echo '<td> n/a </td>';
        } else {
          echo '<td> </td>';
        }
     }
}
echo '</table>';

如果您对jQuery还满意的话,可以考虑使用datagrids。并且是两个非常好的插件。我建议使用jqGrid。有非常好的文档,易于自定义。

如果您想在PHP中使用,请查看usort,并让用户提供字段,以便使用GET变量进行排序。如果您在一个页面中拥有所有数据,那么提供的javascript解决方案既可以减少服务器上的负载,又可以提高用户满意度。没有理由你不能两者兼得

一个例子是不要复制/粘贴,只是为了说明这一点,尽管我确实内置了更常见的安全防护装置:

class KeySorter {
   private $valids;
   private $key;
   function __construct($valids){
        $this->valids = $valids;
        $this->key = reset($this->valids);
   }
   function setKey($key){
       if(in_array($key, $this->valids)) $this->key = $key;
   }
   function compare($a,$b){
      return strcmp($a[$this->key],$b[$this->key]);
   }
   function getValids(){
       return $this->valids;
   }
}
$keys = array('Server', 'Target','Set','Time', 'Length','Size','Status');
$data = array(...your array....);
$sorter = new KeySorter($keys);
if(isset($_GET['sort'])){
     $sorter->setKey($_GET['sort']);
}
usort($data,array($sorter,'compare'));

echo '<table><thead><tr>';
foreach($sorter->getValids() as $sortkey){
     echo '<th><a href="'
       .htmlspecialchars(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), ENT_QUOTES)
       .'?sort='.urlencode($sortkey)
       .'">'.$sortkey.'</a></th>';
}
echo '</tr></thead>';

这就是我倾向于的。我以前没有使用过GET变量。你有一个很好的例子吗?添加一个例子作为说明这很有趣。我以前没有使用过jqGrid。我刚看了演示。设置起来容易吗?只需对一列进行排序?@cjd143SD-对于本地数据也很容易设置。文档非常详尽-