Jump to content

array parsing problem


vzwhaley

Recommended Posts

Hello everyone and thanks much for your past help.

 

I am still trying to work out problems associated with a restaurant directory I am creating. I have two tables - Restaurants and RestaurantsCuisine. The RestaurantsCuisine has two fields, ID and CuisineType, in which I store the autonumber ID and name of the cuisine, such as American, Italian, Mexican, etc. I am storing the values of a restaurants cuisines in a field in the Restaurants table named Cuisine and am storing all of the cuisine IDs in an array with a pipe symbol, such as 1 | 22 | 17, etc.

 

I can successfully explode the Cuisine field and get the individual cuisine ID numbers, but I am designing a jump menu to be able to search by cuisine name from the Restaurants table. So I am wanting to take the cuisine ID numbers in the array and match any restaurant that has a particular cuisine and return just those restaurants back to the search results page.

 

I am trying to dynamically create the SQL statement for the Cuisine Search page by exploding the Cuisine ID numbers and matching the ID of the cuisine passed in the URL to bring back the appropriate restaurant matches to the search page. However, I am having difficulty in getting my code to properly loop through each of the ID numbers in the Cuisine array stored in the Restaurants table.

 

In the following code, I receive the following information from searching for restaurants with a cuisine ID of 22:

 

9 Array ( [0] => 11 [1] => 16 [2] => 22 )

16 Array ( [0] => 6 [1] => 20 )

14 Array ( [0] => 4 )

10 Array ( [0] => 16 )

15 Array ( [0] => 8 )

17 Array ( [0] => 17 [1] => 22 )

 

Here is my code:

 

$sql5 = "SELECT * FROM Restaurants ORDER BY Name ASC";
$Recordset5 = mysql_query($sql5);

$ID = $_REQUEST['ID'];
$str = array();

while ($row_CID = mysql_fetch_assoc($Recordset5)) {

	$tempC = explode(" | ", $row_CID['Cuisine']);
	echo $row_CID['ID'] . " ";
	print_r($tempC );
	echo "<br>";

	if(in_array($ID, $tempC)) {
		$str[] = "ID = " . $row_CID['ID'];				
	} 

	if($str > 0) {
		$str = implode (" OR ", $str);
		}
  
	}

 

The code should give me a SQL statement that looks something like SELECT * FROM Restaurants WHERE ID = 17 OR ID = 9 ORDER BY Name ASC when passing a cuisine ID of 22 to the search page. The problem with the above code is that the SQL statement will sometimes choose one restaurant but not all restaurants who have a value of 22 in the Cuisine array that I am exploding. And sometimes, it will not choose any restaurants, although the ID is plainly in the cuisine array.

 

I just essentially want to be able to parse the contents of the cuisine array in the Restaurants table and pull any restaurants back to the search page that match a particular Cuisine ID passed in the URL to the search page. I know there has to be an easy way to do this. Any suggestions would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/43493-array-parsing-problem/
Share on other sites

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.