Connecting Tech Pros Worldwide Help | Site Map

Problem of sorting table

Ross
Guest
 
Posts: n/a
#1: Aug 23 '05
The following codes originally provided by a kind responder Toby A Inkster
in another newsgroup for working on displaying a table on web capable of
sorting are modified by me (a newbie). How to disable sorting by say, length
and chart (last few codes at the end)? I find the order is important to
display so cannot just single them out.

<?php
function insert_datatable_cmp ($a, $b) {
return ($a[$_GET['sort']]<$b[$_GET['sort']]) ? -1 : 1;
}

function insert_datatable ($data, $headings, $options=array(), $caption='')
{

if ($caption!='' && $options['h2']==1) print
"<h2>$caption</h2>\n";
print "<table>\n";
if ($caption!='' && $options['h2']!=1) print
"<caption>$caption</caption>\n";
print "<thead><tr>";
$i = 0;
foreach ($headings as $h) {
print "<th scope=\"col\"><a
href=\"${_SERVER['PHP_SELF']}?sort=$i\">$h</a></th>";
$i++;
}
print "</tr></thead>\n<tbody>\n";

$lines = explode("\n",$data);
$i = 0;
while ($l = array_shift($lines)) {
$s[$i++] = explode("|",$l);
}

if(isset($_GET['sort'])) {
usort($s,"insert_datatable_cmp");
}

foreach ($s as $S) {
print "<tr>";
for($i=0;$S[$i];$i++)
{
if ($options['email:'.$i]==1)
{
$S[$i] = '<a href="mailto:' . $S[$i]
.. '">' . $S[$i] . '</a>';
} elseif ($options['web:'.$i]==1)
{
$S[$i] = '<a href="' . $S[$i] . '">'
.. $S[$i] . '</a>';
}

if ($options['join:'.$i.':'.($i+1)]==1)
{
print '<td colspan="2">' . $S[$i] .
' ' . $S[$i+1] . '</td>';
$i++;
}
else
{
print '<td>' . $S[$i] . '</td>';
}
}
print "</tr>\n";
}

print "</tbody></table>\n";

}
?>

DATA,

<?php
$d = "A|3|5|1|10|3|<img src=\"fig/fig1.jpg\" width=120 height=90\>
B|5|2|2|4|3|figB.gif
C|4|3|3|9|6|figC.gif";

$h = array('name','size','length','0min','3min','10min' ,'chart');
$o = array('web:6'=>1);
$c = 'My Caption';

insert_datatable($d, $h, $o, $c);
?>



AngelBlaZe
Guest
 
Posts: n/a
#2: Aug 25 '05

re: Problem of sorting table


wow that's some ugly code!

try this:

<?php
function insert_datatable_cmp ($a, $b) {
$column = $_GET['sort'];
return ($a[$column]<$b[$column]) ? -1 : 1;
}

function insert_datatable($data,$columns,$sortable = array(),$caption =
'My Caption')
{
echo "<h2>$caption</h2>";

if(isset($_GET['sort'])) {
usort($data,"insert_datatable_cmp");
}
echo '
<table>
<thead>';
foreach ($columns as $column => $type)
{
if (in_array($column,$sortable))
{
echo str_replace('$column',$column,'<th><a
href="?sort=$column">$column</a></th>');
}else{
echo "<th>$column</th>";
}

}
echo '
</thead>
<tbody>';
foreach ($data as $row)
{
echo '<tr>';
foreach ($row as $col => $item)
{
$type = $columns[$col];
switch ($type)
{
case 'link':
echo str_replace('$item',$item,'<td><a
href="$item">$item</a></td>');
break;
case 'text':
default:
echo "<td>$item</td>";
}
}
echo '</tr>';
}
echo '
</tbody>
</table>';

}

$data = array();
$data[0]['name'] = 'A';
$data[0]['size'] = 3;
$data[0]['length'] = 5;
$data[0]['0min'] = 1;
$data[0]['3min'] = 10;
$data[0]['10min'] = 3;
$data[0]['chart'] = 'fig1.jpg';

$data[1]['name'] = 'B';
$data[1]['size'] = 5;
$data[1]['length'] = 2;
$data[1]['0min'] =2;
$data[1]['3min'] = 4;
$data[1]['10min'] = 3;
$data[1]['chart'] = 'figB.jpg';

$data[2]['name'] = 'C';
$data[2]['size'] = 4;
$data[2]['length'] = 3;
$data[2]['0min'] = 3;
$data[2]['3min'] = 9;
$data[2]['10min'] = 6;
$data[2]['chart'] = 'figC.jpg';

$columns = array();
$columns['name'] = 'text';
$columns['size'] = 'text';
$columns['length'] = 'text';
$columns['0min'] = 'text';
$columns['3min'] = 'text';
$columns['10min'] = 'text';
$columns['chart'] = 'link';

$sortable = explode(',','name,size,length');


insert_datatable($data,$columns,$sortable,'My Own Caption');


?>

Closed Thread