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

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.