scottybwoy Posted February 27, 2007 Share Posted February 27, 2007 Hi, I'm trying to display results of the items users request to process an order, however only part of the info is being displayed. I'm using 3d session arrays with the modelId as the handle. Like so : $_SESSION['item_buy'] => modelId1, modelId2, etc. $_SESSION['item_buy'][modelId1] => price, qty, net, vat, gross. $_SESSION['item_buy'][...etc...] => price, qty, net, vat, gross. This is displayed via my template, section shown below : <table style='width:100%;'> <tr> <th width='30%' style='text-align:center;'>Model Number</th> <th width='10%' style='text-align:center;'>Price</th> <th width='12%' style='text-align:center;'>Quantity</th> <th width='14%' style='text-align:center;'>Net Value</th> <th width='13%' style='text-align:center;'>Vat Value</th> <th width='17%' style='text-align:center;'>Gross Value</th> <th width='4%' style='text-align:center; border-right:0px;'> </th> </tr> <?php foreach ($_SESSION['item_buy'] as $modelId => $val) { static $i; $i++; echo "<tr style='border-top:solid 1px navy;' "; if ($this->isodd($i) == TRUE) { echo "bgcolor='white'><td>" . $modelId . "</td>"; } else { echo "bgcolor='#99CCFF'><td>" . $modelId . "</td>"; } foreach ($_SESSION['item_buy'][$ModelId] as $k => $v) { echo "<td align='center'>$v</td>"; } echo "<td height='9px'><a style='border:0px; background:none;' href='home.php?content=sale&item=$itemId&status=minus'><img alt='minus' border='0' src='../httpd/images/minus.gif' /></a></td></tr>"; } ?> <tr> <th colspan='3' align='right'>Totals</th> <td style='border-right:solid 1.5px navy;'><?php print($total_array['t_net']); ?></td> <td style='border-right:solid 1.5px navy;'><?php echo $total_array['t_vat']; ?></td> <td><?php echo $total_array['t_gross']; ?></td> <td> </td> </tr> </table> The arrays are produced via this code here : <?php global $modelId, $qty, $price; if (empty($_POST['qty'])) { die("You must have at least one item, twat!<br>"); } $modelId = $_POST['modelId']; $qty = $_POST['qty']; $price = $_POST['price']; $stock = PHPApplication::selectRecord('stockAvail', 'products', 'modelId', $modelId); if ($qty > $stock) { "This will produce a back order, items will be shipped in two parts unless arranged otherwise."; } if ($_POST['pay'] == 0) { $prod_net = 12; $prod_vat = round(12 * VAT, 2); $prod_gross = round($prod_vat + $prod_net, 2); $_SESSION['item_buy']['Delivery'] = array(12.00, 1, $prod_net, $prod_vat, $prod_gross); } else { $prod_net = 9; $prod_vat = round(9 * VAT, 2); $prod_gross = round($prod_vat + $prod_net, 2); $_SESSION['item_buy']['Delivery'] = array(9.00, 1, $prod_net, $prod_vat, $prod_gross); } if (is_numeric($qty) && is_numeric($price)) { $prod_net = $qty * $price; $prod_vat = $prod_net * VAT; $prod_gross = $prod_net + $prod_vat; if (array_search($modelId, $_SESSION['item_buy'])) { $o_qty = $_SESSION['item_buy'][$modelId][1]; $qty = $o_qty + $qty; $_SESSION['item_buy'][$modelId][1] = $qty; } else { $_SESSION['item_buy'][$modelId] = array($price, $qty, $prod_net, $prod_vat, $prod_gross); //print_r($_SESSION['item_buy'][$modelId]); } foreach ($_SESSION['item_buy'] as $k => $v) { static $total_net, $total_vat, $total_gross; foreach ($_SESSION['item_buy'][$k] as $key => $val) { if ($key == 3) { $total_net .= $val; } if ($key == 4) { $total_vat .= $val; } if ($key == 5) { $total_gross .= $val; } } } $total_array = array('t_net' => $total_net, 't_vat' => $total_vat, 't-gross' => $total_gross); //print_r($total_array); } else { die("Can't have textual quatities / prices"); } ?> When the print_r is uncommented, it displays the correct data, but for some reason the foreach used in the template does not believe there are any values available. It does print out the Model Id and Delivery <th> but does not print out any of the values, nor does it print out the totals at the bottom of the display, can anyone see why? Sorry for the long post, thanks for your time Quote Link to comment Share on other sites More sharing options...
Orio Posted February 27, 2007 Share Posted February 27, 2007 I don't know if that is what causing the problem, but it caught my eye. Your code starts this way: <?php foreach ($_SESSION['item_buy'] as $modelId => $val) { static $i; $i++; ?> The problem- $i isn't given a value so you'll get strange results. I suppose it should be set to 0 (or 1), no? Orio. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted February 27, 2007 Author Share Posted February 27, 2007 Well no that's not the problem, that is only meant to make each row appear a different colour, which was working fine. Quote Link to comment Share on other sites More sharing options...
Orio Posted February 27, 2007 Share Posted February 27, 2007 I think I understand what's the problem. First try this and tell me if it works, if it does, that means what I think is right: <?php $keys = array_keys($_SESSION['item_buy']); foreach ($keys as $modelId) { static $i; $i++; echo "<tr style='border-top:solid 1px navy;' "; if ($this->isodd($i) == TRUE) echo "bgcolor='white'><td>" . $modelId . "</td>"; else echo "bgcolor='#99CCFF'><td>" . $modelId . "</td>"; foreach ($_SESSION['item_buy'][$ModelId] as $k => $v) echo "<td align='center'>$v</td>"; } echo "<td height='9px'><a style='border:0px; background:none;' href='home.php?content=sale&item=$itemId&status=minus'><img alt='minus' border='0' src='../httpd/images/minus.gif' /></a></td></tr>"; } ?> Orio. Quote Link to comment Share on other sites More sharing options...
scottybwoy Posted February 27, 2007 Author Share Posted February 27, 2007 Ha Ha, yes, thanks Orio, although I had to change $ModelId to $modelId and put in the tilda on the second foreach. I have also got my totals displayed now using a separate $_SESSION array. Cheers for your help. Quote Link to comment Share on other sites More sharing options...
Orio Posted February 27, 2007 Share Posted February 27, 2007 This is what I think happened (I am really not sure tho): On every foreach() call, the internal pointer of the array is automatically reset to the first element. From the manual: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop. So let's look at this array: $_SESSION['item_buy']['modelId1'] = array("price" => 1, "qty" => 7); $_SESSION['item_buy']['modelId2'] = array("price" => 10, "qty" => 85); When the first foreach is called, the array's pointer is on the first element of the array => modelId1. Now, when the inner-foreach is called and finished, the pointer jumps to another position (end of modelId1 maybe?). So when the first foreach (the outside one) runs again, the pointer jumps to another location, and that confuses PHP. I am REALLY not sure about this, but this might be it. Anyway- what's important is that it worked Maybe someone with some more experience and skill could confirm this or explain what's going on. Orio. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.