Page 1 of 1

sorting based on column

Posted: Sun Feb 22, 2004 1:43 pm
by kanshou
this seems like it should be easy, but I'm not sure where to start. I have an HTML table, and several columns, I would like to be able to sort by any column by clicking on the column header and it will sort asc.

Here's what I have as a foundation.

Code: Select all

<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db('phpcollab25beta', $conn) or die(mysql_error());

echo "<body link="#666666" vlink="#000000" alink="#000000">";


if (empty($id)){  // Checks the url to see if there is a value for $id.

	// If not, is lists all entries in the database under that table.
$opened = 0;
$closed = 0;

$stat1 = 1;

	// Status Test 2
	$statresult1 = mysql_query("SELECT status FROM phpcollabsdesk_fc WHERE status = '" . $stat1 . "'");

	$opened = mysql_num_rows($statresult1);

	printf ("     Open Requests: <b>%s</b>  -  ", $opened);

	$stat2 = 0;

	$statresult2 = mysql_query("SELECT status FROM phpcollabsdesk_fc WHERE status = '" . $stat2 . "'");

	$closed = mysql_num_rows($statresult2);

	printf ("Closed Requests: <b>%s</b>.", $closed);

	$logstotal = $opened + $closed;

	printf ("  Total Logs: <b>%s</b>", $logstotal);


	echo ( "<table cellpadding="1" bgcolor="#ffffff" width="100%"><tr><td>");
// This is the start of the table I refer to in my opening paragraph
	echo ( "<table cellpadding="1" cellspacing="1" bgcolor="#99CCEE" width="100%"><tr bgcolor="#1E90FF" align="center"><td><b>Requested By</b></td><td><b>Issue</b></td><td><b>Request</b></td><td><b>Priority</b></td><td><b>Location</b><td><b>Status</b></td><td><font size='4'>&nbsp;</tr>");

	$result = mysql_query("SELECT * FROM phpcollabsdesk_fc");

	$myrow = mysql_fetch_array($result);

	$i = 0; // sets up the counter for coloring the table rows.
	
	do{
			if($i % 2 == 0) //checks to see if the line is an even number
			{

			printf ("<tr valign="center" bgcolor="#EEF7ff"><td><!--<a href="newtest.php?id=%s">-->", $myrow["id"]);

			}else{

			// The whole purpose of this second area is to just change the highlighting of the table

			printf ("<tr valign="center" bgcolor="#DBEDff"><td><!--<a href="newtest.php?id=%s">-->", $myrow["id"]);

			}

			printf ( "%s</a>",$myrow["Requested_By"]);

			printf ( "<td>%s", $myrow["Issue"]);

			printf ( "<td>%s", $myrow["Request"]);

			printf ( "<td><center>%s", $myrow["Priority"]);

			printf ( "<td><center>%s", $myrow["Location"]);

			if($myrow["status"] == 1){  // This "if" statement checks to see if the status of the request is opened or closed.
				$status = "Open";
			}
			else{
				$status = "Closed";
			}
			printf ( "<td><center>%s", $status);
			echo "<td><valign='center'><font size="4">&nbsp;</font>";

	$i++;

	}while  ($myrow = mysql_fetch_array($result));

	echo "</table>";

	}

?>
Any help would be greatly appreciated.

Posted: Sun Feb 22, 2004 2:35 pm
by John Cartwright
When they click on the link you could change the query..

add this into it ORDER `id` or ORDER `date` whateve ru want it to be ordered by

Posted: Sun Feb 22, 2004 2:44 pm
by kanshou
hrmm... a little too open ended for me. I'm not sure how to grab that from the browser... and would I have to do an "if" statement? Would you mind being a little more specific please :)

Thanks

P.S. Please bear with me today, I have a headcold so what should be simple is taking up quite a bit of time.