tags:

views:

38

answers:

2

I have an html table that is populated using a text file only and is basically sorted primarily by a timestamp. I would like the option of also sorting the table data by a column ($keys) called "Server" so I can view sorted data based on the server name. Since this is not using mysql and I can't use an 'order by', is there a way to do accomplish this? How do you do this in php?

Here is some of my code showing how my table is created:

$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>';
A: 

You can look into using datagrids if you are okay with jQuery. Flexigrid (demo) and jqGrid (demo) are two very good plugins for the same. I recommend using jqGrid. Has very good documentation and is easy to customize.

ShiVik
This is interesting. I have not used jqGrid before. I just saw the demo. Would this be easy to set up? Just to sort off one column?
cjd143SD
@cjd143SD - Its quite easy to setup (for local data as well). Documentation is pretty exhaustive - http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jqgriddocs
ShiVik
A: 

If you want to do in in PHP, look into usort, and let the user provide the field to sort by with a GET variable. If you have all data in one page, the javascript solutions provided can both decrease load on your server, and increase user satisfaction. No reason why you can't do both ;)

An example would be (don't copy/paste, just illustration for this purpose, although I did built in the more usual safety guards):

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>';
Wrikken
that's what i would lean towards to. I haven't use GET variable before. Do you have a good example of this?
cjd143SD
added an example as illustration
Wrikken
@Wrikken- thanks. its a good start. see if i can make this work.
cjd143SD