Snooble Posted February 20, 2007 Share Posted February 20, 2007 Hello people, Just a quick one (Hopefully)... I want to have a list of songs to appear on a page in which the user can order them by: Last added Alphabetically(A-Z) Size Do i use MySQL? I know how to print results from mysql but i'd need help with creating a loop that printed the results until there were none left, also, while printing i need them to be put into a table. But the main objective is to learn how to sort the information on the users page. Thanks Snooble Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/ Share on other sites More sharing options...
trq Posted February 20, 2007 Share Posted February 20, 2007 You would need to store any infomation you wish to sort by in mysql. Then, you'd simply use an ORDER BY clause. eg; SELECT songtitle FROM songs ORDER BY songtitle; Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/#findComment-189408 Share on other sites More sharing options...
Snooble Posted February 20, 2007 Author Share Posted February 20, 2007 ok, what would i link to... for example table looks like: Song Title | Size | Date Added | Download Link ---------------------------------------------------------- I'm an elephant| 2.3mb | 20.2.07 | Download When i click song title it orders in alphabetical... so... Order by (name of field) Got it, but how do i link to that command? 2 ways i think of, one... linking to a different .php file with the sql in it. two. sql injection... SELECT * FROM (tablename) ORDER BY (fieldname)... Best way? Snooble (Thank you) Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/#findComment-189411 Share on other sites More sharing options...
trq Posted February 20, 2007 Share Posted February 20, 2007 Just make your links pass the order by clause through the url. eg; <a href="display.php?orderby=title">Song Title</a> Then, on display.php have something like. <?php if (isset($_GET['orderby'])) { $order = $_GET['orderby']; } $sql = "SELECT * FROM songs ORDER BY $order"; ?> Of course you would need to setup defaults and sanitize the input, but yeah, should give you the idea. Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/#findComment-189436 Share on other sites More sharing options...
Snooble Posted February 20, 2007 Author Share Posted February 20, 2007 thank you. never EVER passed information through the URL so it's worth a shot for me. Thank you i understand that code. htmlentities will do wont it? Snooble Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/#findComment-189440 Share on other sites More sharing options...
willpower Posted February 20, 2007 Share Posted February 20, 2007 There is a great way to do this using mod_rewrites. It appears complicated at first but its weel woth the effort. 1.You construct your usrls, based on the information that you want in your queries. ie mysite.com/artist/album/track. 2. You construct the mod_rewrite to read the url and transfer it into good ol fashined url's ie mysite.com/find.php?artiste=this&album=&track=thenext 3. Then you build your SELECT Statement with this information 4. Bobs your uncle you have a user firendly, search engine friendly site with nice clean urls. Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/#findComment-189455 Share on other sites More sharing options...
trq Posted February 20, 2007 Share Posted February 20, 2007 htmlentities will do wont it? Not at all. You'll want to prevent sql injection, the easiest way (in this case) is to limit the order by clauses available. eg; <?php $valid = array('title','date','size'); if (isset($_GET['orderby'])) { $order = in_array($_GET['orderby'],$valid) ? $_GET['orderby'] : 'title'; } else { $order = 'title'; } ?> this will also impliment the default order by to be by title. Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/#findComment-189460 Share on other sites More sharing options...
Snooble Posted February 20, 2007 Author Share Posted February 20, 2007 thank you, i'll change title to date and then it'll show the last uploaded thank you... will post again when i have the table and that running... So far i have www.wezzsmusic.ifastnet.com/WM/music.php i want it to add cells to the table printing the song title etc in each cell... I just need an example for a loop that will pull the information from the mysql table and into the seperate cells (creating a new one)... Snooble Quote Link to comment https://forums.phpfreaks.com/topic/39296-ordering-information/#findComment-189466 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.