bcode Posted October 6, 2009 Share Posted October 6, 2009 I just got this project thrown into my lap and I can't figure out a way to make it work without going in and changing the database around which would be a nightmare Problem- When the output of categories/types ie.(chinese, italian, american) is spit out it is calling from a certain id given to the category and then when the for loop is ran it is spitting them out in numerical order is there a way i can run something in the for loop to spit them out in alphabetical order? $query = "SELECT * FROM types"; $result = mysql_query($query); echo "<p style='text-align:justify; padding:0 1em;'>\n"; while ($row = mysql_fetch_array($result)){[color=red]here is where the problem starts[/color] $i = $row['type_id']; $type[$i] = $row['type']; echo "<a href='#$i'>".$type[$i]."</a> | \n"; } echo "</p>\n"; for($i=30 ; $i<98 ; $i++){ //[color=red]This is where i am having a problem[/color] $query = "SELECT * FROM restaurants WHERE (type LIKE '%$i%') AND (zips LIKE '%$zipcode%') AND (type NOT LIKE '99') ORDER BY name ASC"; $result = mysql_query($query); if (mysql_numrows($result) > 0){ [color=red]This is where i am having a problem too[/color] echo "<a name='$i'></a>\n"; echo "<h4>".$type[$i]."</h4>\n"; while ($row = mysql_fetch_array($result)){ $rest_num = $row['rest_num']; $image = $row['image']; $name = $row['name']; $type_num = $row['type']; $time = $row['time']; echo "<div id='restaurants'>\n"; echo "</div>\n"; } //end while } //end if } //end for Thank you for any help this forum is a lifesaver!!! Quote Link to comment Share on other sites More sharing options...
Jahren Posted October 6, 2009 Share Posted October 6, 2009 This code sorts stuff alphabeticaly already. if you meant you want alpha numerical: $query = "SELECT * FROM restaurants WHERE (type LIKE '%$i%') AND (zips LIKE '%$zipcode%') AND (type NOT LIKE '99') ORDER BY name + 0 ASC"; Quote Link to comment Share on other sites More sharing options...
bcode Posted October 6, 2009 Author Share Posted October 6, 2009 Sorry for the confusion i need the $query = "SELECT * FROM types"; to sort alphabetically and when I add an ORDER BY to it doesnt matter because then in the for loop it is reading it by its ID number. Quote Link to comment Share on other sites More sharing options...
Jahren Posted October 6, 2009 Share Posted October 6, 2009 err, I misread, it's not your fault. SELECT id_type, str_type FROM types ORDER BY str_type ASC change id_type and str_type to your columns' real name Quote Link to comment Share on other sites More sharing options...
bcode Posted October 6, 2009 Author Share Posted October 6, 2009 I tried it and that wont work because then when the for loop is run it overwrites the ORDER BY. I need something that will order it in the FOR loop like sort() or something but I cant get it to work Quote Link to comment Share on other sites More sharing options...
Jahren Posted October 6, 2009 Share Posted October 6, 2009 Hmmm, try seperating the operations to simplify the model. first you want all types in order. then use a temp array to store the data. (declare the array before the first loop.) $query = "SELECT id_type, str_type FROM types ORDER BY str_type ASC"; then I need to understand why $i=30 ; $i<98. is it that you want to loop for each type you get from the first query? Quote Link to comment Share on other sites More sharing options...
Mchl Posted October 6, 2009 Share Posted October 6, 2009 Wow... 69 queries in a for loop... Why don't you retrieve all data outside of the loop, store it into array, and then use loop to echo it out? Quote Link to comment Share on other sites More sharing options...
bcode Posted October 6, 2009 Author Share Posted October 6, 2009 the for loop is looping through and then for each of the types the number gets added on because that is how the restaurants are categorized so lets say type = chinese type_id = 41 then the restaurant is name = peking wok type = 41 so then the restaurant is put in the chinese category but when the categories are spit out they come in as numerical order instead of alphabetical. The restaurants come in ok no problem there but this guy decided to do things where the category is dependant on the type_id for its order placement which isnt very fun Quote Link to comment 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.