Rommeo Posted December 31, 2007 Share Posted December 31, 2007 Hi i have a problem about sorting data that is stored in my DB. Data in the DB is something like this -------menu_order------------------- stuffs-----1---------------- stuffs-----5---------------- stuffs-----4---------------- stuffs-----2---------------- 1- )now i want to sort this data and print it to the screen. Is there anyway to do this ? i heard about there is a sort function but i m not sure about it ? 2-) As you noticed there is no "3". i want to sort whole data and after that i want it like " 1,2,3,4 ".. i have tried this code using arrays ( first time i m using array ) for to print the data... ( if that would work i ll try to sort ) but for to print data this code did not work.. : $i=0; $array=array(); while( $i<num ) { $menu_order = mysql_result($queriedsql,$i,"menu_order"); $array[$i]=$menu_order; echo "$arr[$i]<br/>"; $i++; } // assume i have done sql-check and included db and there is no problem 3-) i was busy with c++ before. And there was stuctures .. Is it same as php ? How can i control whole data like this in DB easily ? I ll be glad if you can answer any of my questions. I'm a newbie so i ll also be glad if your replies are clear. Thanx in advance. Quote Link to comment https://forums.phpfreaks.com/topic/83860-sorting-menu-order-sort-array-structure/ Share on other sites More sharing options...
p2grace Posted December 31, 2007 Share Posted December 31, 2007 Hmm I'm a little confused about what you're asking. If you don't want to sort your data by a specific key or by a timestamp, then you need to add another field like "sortOrder" and sort by that. The way you have yours "1,5,4,2" wouldn't make sense unless you have some key to sort by. Please be more clear with your question so we can assist. Quote Link to comment https://forums.phpfreaks.com/topic/83860-sorting-menu-order-sort-array-structure/#findComment-426798 Share on other sites More sharing options...
revraz Posted December 31, 2007 Share Posted December 31, 2007 In your SQL query, use ORDER BY fieldname. Quote Link to comment https://forums.phpfreaks.com/topic/83860-sorting-menu-order-sort-array-structure/#findComment-426801 Share on other sites More sharing options...
p2grace Posted December 31, 2007 Share Posted December 31, 2007 revraz is correct as long as you want to order by a current fieldname. No matter what you'll want to use order by fieldname, but you might need to add a sortOrder field to order by. Edit: This of course depends on what exactly you want your output to look like. If you want it sorted by title you wouldn't need to add a sort order. But if you wanted to be able to control the sort, you would need to add a sort order field. Quote Link to comment https://forums.phpfreaks.com/topic/83860-sorting-menu-order-sort-array-structure/#findComment-426804 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.