PHP: Sort items at command?

ChiBuki

New Member
Messages
11
Reaction score
0
Points
0
Hi! I want to try something new with this table.

By default it's sorted by ID descending. The command:
PHP:
$query = "SELECT ID, FeaturedCharacter, Rank, Average, TotalVotes, FivePoints, FourPoints, ThreePoints, TwoPoints, OnePoints, DateUpdated, Poll FROM TsundereOrNot ORDER BY ID DESC LIMIT $offset, $rowsPerPage";

I want to make the table headers to be clickable. When I click on "Character", the table should be sorted by the character's name alphabetically. When I click on "Last Updated", the table should be sorted by the last updated date, and so on.... And when I click on "Average", the table should be sorted from the highest number to lowest; but when I click on "Average" again, it would sort from lowest to highest.

Basically I want it to be exactly like this, where you can have the list sorted by whatever is clicked in the table header.

How do I make my table be like that using PHP?

I didn't have luck with finding tutorials on Google, probably because I don't know the keyword/term for it. Thanks. :happysad:
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
Easily.

(this is lifted from my own site so you'll have to change it to fit your own)

Build your SQL query then
PHP:
	if($_GET['sortBy'] != null){
		$sql .= " ORDER BY " . $_GET['sortBy'];
	} else {
		$sql .= " ORDER BY BandList.gigDate";
	}
And at the top of your table put:
PHP:
echo "<tr>";
			echo "<th><a href=\"?sortBy=bandName\">Band</a></th>";
			echo "<th><a href=\"?sortBy=tourName\">Tour</a></th>";
			echo "<th><a href=\"?sortBy=venueName\">Venue</a></th>";
			echo "<th><a href=\"?sortBy=townName\">Town</a></th>";
			echo "<th><a href=\"?sortBy=gigDate\">Date</a></th>";
			echo "<th><a href=\"?sortBy=attending DESC\">Attending?</a></th>";
			echo "<th>Buy Tickets</th>";
		echo "</tr>";
 
Top