Jump to content

For loop sorting data?


bcode

Recommended Posts

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!!!

Link to comment
https://forums.phpfreaks.com/topic/176740-for-loop-sorting-data/
Share on other sites

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?

 

 

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.