eluzion Posted September 25, 2008 Share Posted September 25, 2008 Good evening everyone, I am very new to PHP so bare with me. I'm trying to setup a table that is populated with data from a MySQL table with the ability to sort the data by each column. For example, I click "First Name" on the top of the column and all the data entries are sorted in alphabetical order. I found some forum postings explaining this and I have it partially working. Here is the code: <?php if(isset($_GET['sortby'])) { $sortby = $_GET['sortby']; if($strcasecmp($sortby, "desc")==0) { $newsort = "ASC"; } else { $newsort = "DESC"; } } else { $sortby = "ASC"; $newsort = "DESC"; } if(isset($_GET['orderby'])) { $orderby = $_GET['orderby']; $query = "SELECT * FROM servicelog ORDER BY ".mysql_real_escape_string($orderby); if(strcasecmp($sortby, "desc")==0) { $query.= " DESC"; } else { $query.= " ASC"; } $result = mysql_query($query); } else { $query = 'SELECT * FROM servicelog'; $result = mysql_query($query); } ?> And the column headers are linked with the following: <? echo $_SERVER['PHP_SELF']."?orderby=invoice_number&sortby=$newsort";?> I'm guessing the problem has something to do with this section of the code: if(isset($_GET['orderby'])) { $orderby = $_GET['orderby']; $query = "SELECT * FROM servicelog ORDER BY ".mysql_real_escape_string($orderby); if(strcasecmp($sortby, "desc")==0) { $query.= " DESC"; } else { $query.= " ASC"; } $result = mysql_query($query); } Reason being is that the links are coming out right when I click the header (as in the URL shows the proper ?orderby=invoice_number&sortby=ASC), but when I click them the tables are blank. If think it's just not properly appending the ASC or DESC to the end of the query, but I could be wrong. Anyways, any help would be appreciated! Thanks. Link to comment https://forums.phpfreaks.com/topic/125708-issue-with-sorting-data-in-a-table/ Share on other sites More sharing options...
eluzion Posted September 25, 2008 Author Share Posted September 25, 2008 Bah! Figured it out! The first strcasecamp statement (or whatever you call it) had a $ in front of it... Link to comment https://forums.phpfreaks.com/topic/125708-issue-with-sorting-data-in-a-table/#findComment-650170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.