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

Recommended Answers

All 3 Replies

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 . "'");

Thanks very much. Damn my over complicating mind.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.