Hello everyone, I've been trying to solve this for couple of days now. Here's where I'm at: foreach ($contents as $id=>$qty) { $query = mysql_query("SELECT * FROM items WHERE item_id='$id'") or die(mysql_error()); while ($result = mysql_fetch_array($query)) { //IN CASE IT'S THE FIRST ELEMENT IN ARRAY if(!isset($_SESSION['price_fix'])){ $$result['item_id'] = array($result['item_id'],$qty,$price); $woohoo = $$result['item_id']; //FOR EVERYTHING ELSE }else{ $$result['item_id'] = array($result['item_id'],$qty,$price); $woohoo = ''.$woohoo.','.$$result['item_id'].''; } $_SESSION['price_fix'] = array($woohoo); } } PHP: I need to get array like so: $_SESSION['price_fix'] = array( array(item_id1, $qty1, $price1),array(item_id2, $qty2, $price2),array(item_id3, $qty3, $price3) ); I think I'm on the right track as first element goes in as suppose to. The problem is within else brackets- I can't figure out how to join in properly if there are more than 2 items in array. Hope this makes any sense to you.
Try following code and let me know if you face any difficulty. As per my understanding, I have removed unwanted code. $result = array(); foreach($contents as $id => $quantity) { $rs = mysql_query("SELECT * FROM items WHERE item_id='$id'"); while($row = mysql_fetch_array($rs)) { $result['item_id'] = array($row['item_id'], $quantity, $row['price']); } } $_SESSION['price_fix'] = $result; PHP:
Thanks for help but it doesn't work as it produces following array: Array ( [item_id] => Array ( [0] => 32 [1] => 1 [2] => 35.00 ) ) and takes into array only last item out of three.
Okay, following should add all elements in array. $result = array(); foreach($contents as $id => $quantity) { $rs = mysql_query("SELECT * FROM items WHERE item_id='$id'"); while($row = mysql_fetch_array($rs)) { $result[] = array($row['item_id'], $quantity, $row['price']); } } $_SESSION['price_fix'] = $result; PHP:
Ok, it's as simple as 1,2,3... Everything as in my code except whole else statement suppose to look like this: else{ $$result['item_id'] = array($result['item_id'],$qty,$price); array_push($woohoo, $$result['item_id']); print_r ($woohoo); } $_SESSION['price_fix'] = $woohoo; PHP: Hope this will help someone.