almystersv Posted January 10, 2008 Share Posted January 10, 2008 Hi Guys, I have this table in place that looks at the previous messages that have been left by other users. At the moment is automatically sorted by date. But I was wondering if it was possible to allow the user to click on the header of each column which would then sort the table by that column. For example... If it had the headings Date, username, Input Type etc.. then if they were to click on the heading Input Type it would sort all the records in that table by the input type! Here is my code for the message page... <?php session_start(); if (isset($_SESSION['username']) == false){ header("Location: login.php"); exit(); } require "connect.php"; ?> <!DOCTYPE HTML PUBLIC "//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>BIS Portal</title> </head> <body> <?php include ("header.php"); ?> <p>Messages</p> </h2> <table align="right" border="1" name="Messages Table" id="1" tabindex="1" color ="#1E1EC8" width="100%"> <tr> <th width="8%" align="left" ><h3>Message Number</h3></th> <th width="21%" align="left" ><h3>Title</h3></th> <th width="42%" align="left" ><h3>Content</h3></th> <th width="15%" align="left" ><h3>Date</h3></th> <th width="14%" align="left" ><h3>User</h3></th> </tr> <?php $query = "SELECT * FROM message, employee WHERE message.empID = employee.empID ORDER BY messageID"; $result = mysql_query($query, $connection) or die ("MySQL Error: ".mysql_error()); while($row = mysql_fetch_array($result)) { ?> <tr> <td align="center"><?php echo $row['messageID']?></td> <td><?php echo $row['msgTitle']?></td> <td><?php echo $row['msgBody']?></td> <td><?php echo $row['msgDate']?></td> <td><?php echo $row['fName']?> <?php echo $row['sName']?></td> </tr> <?php } ?> </table> </h4> </body> </html> Cheers Quote Link to comment https://forums.phpfreaks.com/topic/85399-solved-sorting-tables-by-different-columns/ Share on other sites More sharing options...
interpim Posted January 10, 2008 Share Posted January 10, 2008 I would throw the user selected column back into the page as a variable in the GET array. Then you could change your query ORDER BY attribute based on what they select. Quote Link to comment https://forums.phpfreaks.com/topic/85399-solved-sorting-tables-by-different-columns/#findComment-435735 Share on other sites More sharing options...
almystersv Posted January 10, 2008 Author Share Posted January 10, 2008 Is that difficult to do...!? Sorry. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/85399-solved-sorting-tables-by-different-columns/#findComment-435737 Share on other sites More sharing options...
interpim Posted January 10, 2008 Share Posted January 10, 2008 make the column header links... back to the same page. But instead of http://website.com/table.php it will be http://website.com/table.php?var=user Then you can pull the value after var into the GET array, and assign that value to your query. Quote Link to comment https://forums.phpfreaks.com/topic/85399-solved-sorting-tables-by-different-columns/#findComment-435741 Share on other sites More sharing options...
almystersv Posted January 10, 2008 Author Share Posted January 10, 2008 Ok, still don't really understand how to get the variables from there into the array but thank you for your time! Quote Link to comment https://forums.phpfreaks.com/topic/85399-solved-sorting-tables-by-different-columns/#findComment-435771 Share on other sites More sharing options...
interpim Posted January 10, 2008 Share Posted January 10, 2008 You get the variables into the array by way of the URL say your page address is www.mysite.com/page.php add a ? then a variable name say var, then = and then the value you want to assign that variable... So www.mysite.com/page.php?var=user would be a case of this... now, in your code $orderedby = $_GET['var']; $orderedby will now contain the value "user" or whatever is following the = sign in your url. So, essentially you will want to build the column headers as links. so for each link it will be the same except for the variable at the end. your query will then use that variable in it's order by section to determine what to order the query by. so where you have "ORDER BY messageID" you would make it "ORDER BY $orderedby" and the value of your variable will change the way your data is sorted. Quote Link to comment https://forums.phpfreaks.com/topic/85399-solved-sorting-tables-by-different-columns/#findComment-435814 Share on other sites More sharing options...
almystersv Posted January 10, 2008 Author Share Posted January 10, 2008 Superb. Sorry for having to explain so simply for me! Legend! Quote Link to comment https://forums.phpfreaks.com/topic/85399-solved-sorting-tables-by-different-columns/#findComment-435828 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.