473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problem of sorting table

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);
?>

Aug 23 '05 #1
1 1620
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');
?>

Aug 25 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
7
by: Foxster | last post by:
in access i have two tables (tblplayer, tblpoints) in tblplayer i have field totalpoint (number) in tlbpoints i have field pointgame (number) the two files are related via idplayer now i made a...
7
by: manning_news | last post by:
I've got a report that's not sorting correctly. I build a SQL statement and assign it to the recordsource in the Open event, sorting the data the way the user chooses. The user can choose up to 3...
3
by: Neil Hindry | last post by:
I wonder if you can help me. I have setup an address-book database in Access XP. I have the first name & surname as separate fields. As I wanted to sort my database by surname and then by first...
20
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
2
by: Bob Laubla | last post by:
Hello I have a very complex maketable query with many records and involving multiple VB functions which call other functions. I need this table to be sorted by the first field. But no matter what...
1
by: sheldonlg | last post by:
I have inherited code with a TDC control. In this file, there are two javascripts of interest. One of these is a function, filter(), which is inside <script language=javascript></script>. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.