0

Hi,

I have got 2 arrays; one hold ingredient and the other hold measurement. how would i go through and list the ingredient with its corresponding measurement in the other array until i have listed all the ingredients for the recipe. here is the code i currently have. I have tried while statements and it lists the first ingredient but then lists all the measurements and then the remaining ingredients.

$mysqlResult3 = mysql_query("SELECT name
FROM ingredient JOIN recipeingredient ON ingredient.ingredientid = recipeingredient.ingredientid
WHERE recipeingredient.recipeid = '" . $recipeid . "'");
$mysqlArray3 = mysql_fetch_assoc($mysqlResult3);//if the query should be 1 row

$mysqlResult4 = mysql_query("SELECT quantity
FROM recipeingredient WHERE recipeingredient.recipeid = '" . $recipeid . "'");
$mysqlArray4 = mysql_fetch_assoc($mysqlResult4);//if the query should be 1 row
3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by cereal
2

Can't you just join the two queries and use a single loop ?

mysql_query("SELECT name,quantity
FROM ingredient JOIN recipeingredient ON ingredient.ingredientid = recipeingredient.ingredientid
WHERE recipeingredient.recipeid = '" . $recipeid . "'");
0

Just as side note, if you can't join two tables but still need to print through php, you can use mysql_data_seek():

<?
# ...
# connect to db
# ...

$q1 = mysql_query("select * from table_name");
$q2 = mysql_query("select * from table_name_2");

$a = mysql_num_rows($q1)-1;
$b = mysql_num_rows($q2)-1;

$c = ($a == $b) ? $a : max($a,$b);

for($i = 0; $i < $c; $i++)
{
    if($a >= $i)
    {
	if(mysql_data_seek($q1, $i))
	{
	    $r = mysql_fetch_assoc($q1);
            echo 'q1: ' . $r['field_name'] . "\n";
	}
    }

    if($b >= $i)
    {
        if(mysql_data_seek($q2, $i))
        {
            $v = mysql_fetch_assoc($q2);
            echo 'q2: ' . $v['field_name'] . "\n";
	}
    }
}

?>

Bye.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.