Jump to content


Order by column head


2 replies to this topic

#1 cityhunter

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 29 November 2006 - 02:29 PM

Hello, I've only been using php & mysql for a few months now, but I've stumbled upon a nasty little newbie problem. Could someone please help me?

i'm making a to-do list. Every item in this list is nicely put into a table row and sorted by deadline. But i would like to be able for users to order the items by all the fields in my table. As you can see from my code, there are three categories: opdrachtgever, opdrachtnaam and deadline. It would be grand if users could sort this table by clicking on the column heads: opdrachtgever, opdrachtnaam, deadline.

here is my code:

<?php 

$sql = "SELECT `ID`, `opdrachtgever` , `opdrachtnaam` , `deadline` FROM `opdracht` ORDER BY 'deadline' ASC";

$result	= mysql_query($sql);



		echo 	"<table><tr><td><strong>opdrachtgever</strong></td>		
				<td><strong>opdrachtnaam</strong></td>
				<td><strong>deadline</strong></td>
				</tr>";			
											
											
while ($rij = mysql_fetch_array($result)) {
				
	$id = 	 $rij['ID'];
			
	echo 	"<tr><td>" . $rij['opdrachtgever'] . "</td>";
	echo 	"<td>" . $rij['opdrachtnaam'] . "</td>";
	echo 	"<td>" . $rij['deadline'] . "</td></tr></table>";				
	}														
?>

Edited by Mr. Matt, 29 November 2006 - 03:14 PM.


#2 cityhunter

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 29 November 2006 - 03:57 PM

thank you very much, it works great!

#3 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 30 November 2006 - 04:54 PM

But first be sure to validate the incoming data, since it is included in a SQL query. Also, verify it is exactly what you want, by doing something like this.
Though, in this sample, it wouldn't really be nessesary, since if it doesn't match the string exactly, it will default to 'id'.

$orders = array('opdrachtgever', 'opdrachtnaam', 'deadline');

$sort = ($_GET['sort'] && in_array($_GET['sort'], $orders)) ? $_GET['sort'] : 'id';

:ph34r:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users