Jump to content

[SOLVED] Array Display, not showing values


scottybwoy

Recommended Posts

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 ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.