Jump to content

Recommended Posts

Hi guys

 

Here's my problem...

 

I have a table called food (id_food  |  food)

and a table called restaurants  (id_restaurant  |  name  | id_food)

 

I'm storing the id_food as an array in table restaurants like this (1,5,8,10)

 

What I want to do is show the results on restaurant page that show me the array on id_food in table restaurants.  But not show the id numbers but the food name stored on table food.

 

Thanks for the great help!!!

Link to comment
https://forums.phpfreaks.com/topic/74766-help-with-an-array/
Share on other sites

Given the vague description without a real coding example you want to use the foreach loop www.php.net/foreach

 

Whether it needs to be nested or not I am un-sure, but you also want the food table to be in it's own associative array, or store the name of the food with the id in the restaurants table.

 

If you store just the ID in the resturaunts table and the food has it's own master array that is associative by the id, you can simply call

 

$food[$id]  which should print out the name, given the food array structure is like:

 

$food = array(1 => "Pizza", 34 => "Candy");

 

Then $food[1] would print "Pizza".

 

If you want better help, paste current code with the array structures.

Link to comment
https://forums.phpfreaks.com/topic/74766-help-with-an-array/#findComment-377988
Share on other sites

Here's I have so far:

 

<?php //explode the array
               $dbtypes = $row_edit['id_food'];
               $db_array = explode(',', $dbtypes);
   
	$sql = "SELECT id_food, food FROM food ORDER BY food";
	$res = mysql_query($sql) or die(mysql_error());

		echo '<table width="100%" border="0" cellspacing="0" cellpadding="3">';
	$count = 0;
	while (list($id_food, $food) = mysql_fetch_row($res)) {
    		      if ($count % 4 == 0) echo '<tr>';
    		      echo "<td>$food</td>";
    		      $count++;
    		      if ($count % 4 == 0) echo '</tr>';
	}
	// if we got to the end but haven't closed the row
	if ($count % 4 != 0) echo '</tr>';
		echo '</table>';
?>

 

If you see the page result:

 

http://www.costaricaconcierge.com/users/restaurantes_detail.php?dia=25&mes=10&ano=2007&id_local=90

 

I just want to show the array id_food stores on table restaurant.  $food should be the array stored on restaurant.id_food

Hope this clarify my question.

 

Thanks again

Link to comment
https://forums.phpfreaks.com/topic/74766-help-with-an-array/#findComment-378023
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.