stirrah Posted January 6, 2014 Share Posted January 6, 2014 Hey! I have two querys that are fetching data. Each query is populating a html-table. I want to be able to order this data if i click on the header. I've managed to get this to work, BUT I want the first table to "remember" its sorting even if I sort the other table. Look at the attached picture. This is the code for the link: <th><?php echo "<a href='my_projects.php?order=" . 'projectId' . "'>" . 'ID' . "</a>"; ?></th> This is the code for GET value: $order = $_GET["order"]; if (empty($order)) { $order = 'projectId'; } So I think my problem is that every time I reload the page, the value gets resetted to 'projectId'...how can I make the page remember? Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/ Share on other sites More sharing options...
iRoot121 Posted January 6, 2014 Share Posted January 6, 2014 Hmm, is it an option to use cookies? Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/#findComment-1464075 Share on other sites More sharing options...
stirrah Posted January 6, 2014 Author Share Posted January 6, 2014 I guess so? I'm don't really know what that would mean. What's the drawback? Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/#findComment-1464077 Share on other sites More sharing options...
iRoot121 Posted January 6, 2014 Share Posted January 6, 2014 I don't think there is one. Except that the table needs to be sorted again after a cache clean. Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/#findComment-1464078 Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 HTTP is stateless protocol nothing is remembered during HTTP requests. You'd use a cookie (or a session) to remember what table column to sort by. Something like // default order $order = 'projectId'; // is order set in cookie? if (isset($_COOKIE['order')) { // use order in cookie $order = $_COOKIE['order']; } // get order from url? elseif(isset($_GET['order']) { // get order from url $order = $_GET['order']; // remember the column to order the results by. setcookie('order', $order, (time()+3600*24*365)); // Max lifspan 1 year } Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/#findComment-1464082 Share on other sites More sharing options...
Barand Posted January 6, 2014 Share Posted January 6, 2014 Check if the $_GET value is set before checking for the $_COOKIE value otherwise the user can't change it once the cookie is set. Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/#findComment-1464084 Share on other sites More sharing options...
Ch0cu3r Posted January 6, 2014 Share Posted January 6, 2014 @Barand good catch Code amended // default order $order = 'projectId'; // get order from url? if(isset($_GET['order']) { // get order from url $order = $_GET['order']; // remember the column to order the results by. setcookie('order', $order, (time()+3600*24*365)); // Max lifspan 1 year } // is order set in cookie? elseif (isset($_COOKIE['order')) { // use order in cookie $order = $_COOKIE['order']; } Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/#findComment-1464088 Share on other sites More sharing options...
mac_gyver Posted January 6, 2014 Share Posted January 6, 2014 or you could just use $_GET parameter(s), a separate one for each different filter/sort input. Link to comment https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/#findComment-1464089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.