Kay1021 Posted May 13, 2009 Share Posted May 13, 2009 I have a table that contains data from my database. I was wondering if there was a way that I could make the headers (name, date joined, expiry date and fee) control the order of the data in both ascending and descending order. And make the which header is organizing the data currently look different (ie. when data is organized by name...name is bold while other headers are not). I've tried searching for how to do this...but have had no luck with actually accomplishing it. I'd appreciate any help. Thanks Here is my code: <?php print' <table class="myTable"> <thead> <th scope="col"><h2>Name</h2></th> <th scope="col"><h2>Phone Number</h2></th> <th scope="col"><h2>Email</h2></th> <th scope="col"><h2>Date Joined</h2></th> <th scope="col"><h2>Expiry Date</h2></th> <th scope="col"><h2>Fee Paid</h2></th> <th scope="col"><h2>Edit</h2></th> <th scope="col"><h2>Delete</h2></th> </thead>'; $sql = "SELECT name, phone, email, date_joined, expiry_date, fee FROM customer ORDER BY name"; $qry = mysql_query($sql); if (mysql_num_rows($qry) > 0) { while ($rs = mysql_fetch_assoc($qry)) { print' <td>' . $rs['name'] . '</td> <td>' . $rs['phone'] . '</td> <td>' . $rs['email'] . '</td> <td>' . $rs['date_joined'] . '</td> <td>' . $rs['expiry_date'] . '</td> <td>' . $rs['fee'] . '</td> <td><a href="edit.php">Edit</a></td> <td><a href="delete.php">Delete</a></td><tr>'; } } else { print '<h4>no result found</h4>'; } print' </table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/ Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 You would just change ORDER BY to whatever you want to sort by, right? Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/#findComment-833418 Share on other sites More sharing options...
Kay1021 Posted May 13, 2009 Author Share Posted May 13, 2009 But i won't know what the user is going to choose and it will always change Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/#findComment-833452 Share on other sites More sharing options...
BobcatM Posted May 13, 2009 Share Posted May 13, 2009 Something like.. $query="SELECT * FROM whatever ORDER BY $col DESC"; In your table <th scope="col"><h2><a href="pagename.php?col=name">Name</a></h2></th> <th scope="col"><h2><a href="pagename.php?col=phone">Phone Number</a></h2></th> etc.. Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/#findComment-833462 Share on other sites More sharing options...
Ken2k7 Posted May 13, 2009 Share Posted May 13, 2009 Where $col = $_GET['col'] in BobcatM's example. Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/#findComment-833470 Share on other sites More sharing options...
BobcatM Posted May 13, 2009 Share Posted May 13, 2009 Thanks Ken, forgot to mention that. $col = (isset($_GET['col']) ? $_GET['col'] : 'name'); Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/#findComment-833475 Share on other sites More sharing options...
Kay1021 Posted May 13, 2009 Author Share Posted May 13, 2009 Thanks...i'll give it a try Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/#findComment-833512 Share on other sites More sharing options...
Kay1021 Posted May 13, 2009 Author Share Posted May 13, 2009 Thanks it worked. Just another question...if there way that if they click again the header can be order ASC or is that more difficult or needs javascript? thanks Quote Link to comment https://forums.phpfreaks.com/topic/157999-control-the-organization-of-data-table-by-headers/#findComment-833534 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.