PDA

View Full Version : nested while/foreach loop question


kaykay
02-01-2010, 12:00 PM
Hi - first time poster here.

I've been stuck on a problem for about two days and I can't figure out the solution.

I'm using FPDF, but my problem is mainly PHP/MySQL based.

I've got some code that has a nested FOREACH and WHILE loops in it. Now the code seems to running almost perfectly, except that the result of the WHILE loop doesn't clear once it has been run through twice. So essentially the result of the WHILE loop is the current result + previous result

$y=$inv_group_array_grab_unique; //grabs a unique array (normally something like "1,2,5,22,65")
foreach ($y as $inv_group_id)
{

//Inventory Fetching Code
$inventory_fetch_sql = "SELECT * FROM inventory WHERE supplier = '$supplier_id' AND inv_group = '$inv_group_id' "; //grabs the data that I want to display
$inventory_fetch = mysql_query($inventory_fetch_sql,$con);

//Next step
while($row2 = mysql_fetch_array($inventory_fetch))
{

$ingredient_get = $row2["inv"];

$column_inventory_item = $column_inventory_item.$ingredient_get."\n";
}

$pdf->SetFont('Arial','',8);
$pdf->Multicell(100,4,"Group name to go here",1);
$pdf->SetFont('Arial','',8);
$pdf->MultiCell(125,4,$column_inventory_item,1);



//GROUP LOOP ENDS HERE
}

the result comes out like this:

Group 1:
***Loop 1's results***
Group 2:
***Loop 1's results***
***Loop 2's results***
Group 3:
***Loop 1's results***
***Loop 2's results***
***Loop 3's results***

when it should look like:
Group 1:
***Loop 1's results***
Group 2:
***Loop 2's results***
Group 3:
***Loop 3's results***

i'm lost...

does anyone here have any ideas?
Thanks

Fumigator
02-01-2010, 04:10 PM
It's your $column_inventory_item variable. You are adding to it inside the while loop, but you never clear it out. Just add unset($column_inventory_item) just above the while loop and that'll fix it.

kaykay
02-02-2010, 12:34 AM
it worked! thanks

I was using unset() but I wasn't referencing the correct variable

thankyou!