0

I am having an issue when trying to present some information coming from an array.

I have a foreach that checks the n sessions in my site, then I call the database for each session and load an array of items.

After coming out of the foreach, I would like to use the same array sequentially to show the items one by one again, however I am only seeing the last of the items n times.

Essentially, in the second foreach, the $row2['name'] and $row2['price'] are only showing n number of times but always the last item of the table.

foreach ($_SESSION['cart'] as $item) { $pid = $item['itemId']; $q = $item['qty']; if($q==0) continue; $query2 = $con -> prepare("SELECT * FROM item_descr WHERE id_item = :idItem"); $query2-> bindValue (':idItem',$pid); $query2->execute(); $row2 = $query2->fetch(PDO::FETCH_ASSOC); SOME HTML STUFF.... } 

SOME INDEPENDENT HTML STUFF HERE:

foreach ($_SESSION['cart'] as $item) { $pid = $item['itemId']; $q = $item['qty']; HTML <div class="subTotalItem"> <span class='cartItemsText'><?php echo **$row2['name']**; ?></span> <span class='cartItemsText2'><?php echo $q." x "." $".$**row2['price'];** $subTotal+= $row2['price'] * $q; ?></span> </div> } 

Any idea of where the issue might be?

1
  • After the first foreach completes, $row2 will contain the row retrieved from the last query. You need to either run the queries again or consolidate your foreach loops into one. Commented Apr 9, 2013 at 20:28

4 Answers 4

2

Store your "stuff to display later" in a string and echo it where you need.

$subtotalStuff = ""; foreach ($_SESSION['cart'] as $item) { $pid = $item['itemId']; $q = $item['qty']; if($q==0) continue; $query2 = $con -> prepare("SELECT * FROM item_descr WHERE id_item = :idItem"); $query2-> bindValue (':idItem',$pid); $query2->execute(); $row2 = $query2->fetch(PDO::FETCH_ASSOC); $subtotalStuff .= "<div class='subTotalItem'><span>" . $row2['name'] . "</span><span>" . $q . " x " . " $" . row2['price'] . "</span></div>\r\n"; $subTotal+= $row2['price'] * $q; SOME HTML STUFF.... } echo $subtotalStuff; echo $subTotal; 
Sign up to request clarification or add additional context in comments.

2 Comments

$subtotalStuff is something that should be displayed n times with a foreach, that's why there should be 2 foreach
$subtotalStuff is a concatenation of all n rows, so you dont need 2 foreach
0

Isn't the default option for prepared statements with PDO configured as PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY? If I'm right, that means you'd have to run the same query again to compose the HTML part of your code.

I have no experience with reversible cursors, but there's an option for it.

Comments

0

I can only assume you are somehow SETTING the $_SESSION['cart'] variable somewhere in your omitted "SOME HTML STUFF" section. I say this because looking at your code, I would expect $_SESSION['cart'] to have the same exact values for the second foreach as it did for the first foreach.

Comments

0

You should print out in the same loop as the query, $row2 is reset every time it goes through the loop.

foreach ($_SESSION['cart'] as $item) { $pid = $item['itemId']; $q = $item['qty']; if($q==0) continue; $query2 = $con -> prepare("SELECT * FROM item_descr WHERE id_item = :idItem"); $query2-> bindValue (':idItem',$pid); $query2->execute(); $row2 = $query2->fetch(PDO::FETCH_ASSOC); SOME HTML STUFF.... Put your HTML output here, not in second loop } 

This should work

1 Comment

It's 2 separate HTML areas of the page, I need to separate the foreaches since otherwise my html would not print correctly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.