Next MySQL/PHP problem

Permabanned
Joined
19 Apr 2006
Posts
2,333
Location
West Yorkshire
ok here's what I wanna do but cant get my head round it.

I have a table with [ Name | Squad ] as the headers, then it list the people from a mysql db below.

What I want is so that the table headers are clickable and it sorts ascending by that field and if clicked again it sorts descending etc.

I just cant wrap my head round how to do it, I could do it with multiple pages one for each thing, but that isnt really what I want :/
 
This is untested and I didn't put it in something to highlight my syntax so maybe spelling errors etc, but I should be working on my own php stuff right now so this should at least give you a hint of what do do ;)


mypage.php contents:

Code:
<?php

if($_GET['ob']=='d'){$orderBy='DESC';}else{$orderBy='ASC';}

if($_GET['of']=='name'){$orderField='Name';}else{$orderField='Squad';}

$sql="SELECT `Name`,`Squad` FROM `table` ORDER BY $orderField $orderBy";

//ETC ETC

echo "<hr>The SQL was: ".$sql."<hr>";

?>

<a href="mypage.php?ob=d&amp;of=name">Order by NAME descending</a>
<a href="mypage.php?ob=a&amp;of=name">Order by NAME ascending</a>
<a href="mypage.php?ob=d&amp;of=squad">Order by SQUAD descending</a>
<a href="mypage.php?ob=a&amp;of=squad">Order by SQUAD ascending</a>
 
Last edited:
Those ifs could be a lot nicer with some ternary operators:

Code:
$orderBy    = ($_GET['ob'] == 'd')    ? 'DESC' : 'ASC';
$orderField = ($_GET['of'] == 'name') ? 'Name' : 'Squad';
 
I have always avoided using those... I don't know why, maybe it's a phobia :p Either way, they both do the same thing. I suppose the ternary ones are faster to type, though :D

*pokes self* I gotta get on with work... damn forum addiction
 
Back
Top Bottom