SpireLink Posted September 21, 2007 Share Posted September 21, 2007 [code]as we all have saw in forums, like when you get the members list it allows you to select the way you want the data to be listed Member Name DESC Member Name ASC Member ID DESC Member ID ASC I wanted want to use this future in my site the way i know to sort data is doing it in the query, like this [code]$dinings = mysql_query("SELECT * FROM dinings ORDER BY dining_id DESC",$db); I have thinked to it with functions [/code] function sort_name_desc { } function sort_name_asc { } function sort_id_desc { } function sort_id_asc { } [/code] now even if am going to do it like this, i dont want the users to get to know what is the fucntion that I am using , or if there is a better and easier way to do it, because with functions my file will have the same code four times with differnt sort types Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/ Share on other sites More sharing options...
trq Posted September 21, 2007 Share Posted September 21, 2007 because with functions my file will have the same code four times with differnt sort types No need for a seperate function for each type. You simply need to make the sort by links pass a perameter via the url, then sort by that within your query. eg; <?php if (isset($_GET['sortby'])) { $sortby = $_GET['sortby']; } else { $sortby = 'name'; // sort by name as default. } if (isset($_GET['sortorder'])) { $sortorder = $_GET['sortorder']; } else { $sortorder = 'DESC'; // order by DESC as default. } $sql = "SELECT * FROM users ORDER BY $sortby $sortorder"; // go ahead and execute the query. ?> PS: Don't forget to escape any data that may be manipulated by your users. Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-352104 Share on other sites More sharing options...
SpireLink Posted September 21, 2007 Author Share Posted September 21, 2007 because with functions my file will have the same code four times with differnt sort types No need for a seperate function for each type. You simply need to make the sort by links pass a perameter via the url, then sort by that within your query. eg; <?php if (isset($_GET['sortby'])) { $sortby = $_GET['sortby']; } else { $sortby = 'name'; // sort by name as default. } if (isset($_GET['sortorder'])) { $sortorder = $_GET['sortorder']; } else { $sortorder = 'DESC'; // order by DESC as default. } $sql = "SELECT * FROM users ORDER BY $sortby $sortorder"; // go ahead and execute the query. ?> PS: Don't forget to escape any data that may be manipulated by your users. aha, you mean like tha pagenation way right ? now regarding PS: Don't forget to escape any data that may be manipulated by your users. what do you mean by that ?? i didnt get you .. Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-352121 Share on other sites More sharing options...
darkfreaks Posted September 21, 2007 Share Posted September 21, 2007 he meant use mysql_real_escape_string Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-352122 Share on other sites More sharing options...
SpireLink Posted September 23, 2007 Author Share Posted September 23, 2007 he meant use mysql_real_escape_string Thanks for clarification, but at the moment I dont get data posted by users, its only the admins who will be able to post data,users section will be done after final testing of the program with the current services which doesnt include members section untile now Any how, I have learned something new from you guys, thanks a bunch Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-353320 Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 Another way is automate the sort order. Store the current $sortby in a session var. If the new $sortby is different from the session sortby, assume ASC by default. If the new $sortby is the same as session value, toggle the sort order between ASC and DESC. Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-353325 Share on other sites More sharing options...
SpireLink Posted September 23, 2007 Author Share Posted September 23, 2007 Another way is automate the sort order. Store the current $sortby in a session var. If the new $sortby is different from the session sortby, assume ASC by default. If the new $sortby is the same as session value, toggle the sort order between ASC and DESC. My biggest probleme is with sessions as am new to it, would you please explain how to do it with seasons? Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-353350 Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 something like <?php session_start(); // required at top of any script using "$_SESSION" if (isset($_GET['sortby'])) { $sortby = $_GET['sortby']; } else { $sortby = 'name'; // sort by name as default. } $sortorder = 'ASC'; // default if (isset($_SESSION['sortby'])) { if ($sortby == $_SESSION['sortby']) { $sortorder = ($_SESSION['sortorder'] == 'ASC') ? 'DESC' : 'ASC'; } } /** * save session vars */ $_SESSION['sortby'] = $sortby; $_SESSION['sortorder'] = $sortorder; $query = "SELECT * FROM tablename ORDER BY `$sortby` $sortorder"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-353403 Share on other sites More sharing options...
SpireLink Posted September 24, 2007 Author Share Posted September 24, 2007 because with functions my file will have the same code four times with differnt sort types No need for a seperate function for each type. You simply need to make the sort by links pass a perameter via the url, then sort by that within your query. eg; <?php if (isset($_GET['sortby'])) { $sortby = $_GET['sortby']; } else { $sortby = 'name'; // sort by name as default. } if (isset($_GET['sortorder'])) { $sortorder = $_GET['sortorder']; } else { $sortorder = 'DESC'; // order by DESC as default. } $sql = "SELECT * FROM users ORDER BY $sortby $sortorder"; // go ahead and execute the query. ?> PS: Don't forget to escape any data that may be manipulated by your users. aha, you mean like tha pagenation way right ? now regarding PS: Don't forget to escape any data that may be manipulated by your users. what do you mean by that ?? i didnt get you .. Thanks, it works as I want it, now a small dout please , how can i make it toggle between ASC and DESC , like if its the current USED link was users.php?sortby=id&sortorder= DESC the NEW link will be users.php?sortby=id&sortorder= ASC and vice versa Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-353778 Share on other sites More sharing options...
jagat21 Posted September 24, 2007 Share Posted September 24, 2007 you can do something like this : if(isset($_GET['sortorder']) && $_GET['sortorder']=='ASC') { echo'<a href="users.php?sortby=id&sortorder=DESC">Sort By Descending Order</a>'; } else if(isset($_GET['sortorder']) && $_GET['sortorder']=='DESC') { echo'<a href="users.php?sortby=id&sortorder=ASC">Sort By Ascending Order</a>'; } Quote Link to comment https://forums.phpfreaks.com/topic/70102-solved-results-sort/#findComment-353789 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.