making html table columns optionally sortable

Posted by jdamae on Stack Overflow See other posts from Stack Overflow or by jdamae
Published on 2010-12-23T22:22:05Z Indexed on 2010/12/24 0:54 UTC
Read the original article Hit count: 263

Filed under:
|
|
|

I have an html table that is populated from a text file, formatted and semi colon separated. I would like the user to have the option of sorting alphabetically with either of the columns when clicking on the column name (header).

How do I do this in php?? Or is there another way of doing this? Thanks for your help.

Sample raw data/input looks like this:

TYPE=abc;PART=georgetown;FILE=goog_abc.dat.2010122211.gz
TYPE=xyz;PART=ucny;FILE=aol_xyz.dat.2010122209.gz

Php code for table:

$lines = preg_split('~\s*[\r\n]+\s*~', file_get_contents('/temp/tab.txt'));
foreach($lines as $i => $line) {
    $pairs = explode(';', $line);
    foreach($pairs as $pair) {
        list($column, $value) = explode('=', $pair, 2);
        $columns[$column] = true;
        $rows[$i][$column] = $value;
    }
}
$columns = array_keys($columns); 
echo '<table><thead><tr>';
foreach($columns as $column) {
    echo '<th>'.$column.'</th>';
}
echo '</tr></thead><tbody>';
foreach ($rows as $row) {
    echo '<tr>';
    foreach($columns as $column){
               echo '<td>'.$row[$column].'</td>';
    }
    echo '</tr>';
}
echo '</tbody></table>';

© Stack Overflow or respective owner

Related posts about php

Related posts about html