Jump to content

Problem holding value in variable


abbos

Recommended Posts

Hi,

 

I'm having a problem holding a value in a variable and then reloading the page. It is for a shopping cart but I wish to add a field that allows me to manually enter a value for "Labour". I've aded the field but everytime I click on my Update button, it forgets the value I've entered. I can change the quantity values for the items in the cart and this works fine.

 

Here is my code:

 

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="estimate.php?action=update" method="post" id="cart">';
	$output[] = '<table border="0" width="100%" align="center">';
	$output[] = '<td><strong>Our Ref</strong>:<input type="text" name="ref" /><br/><br/></td>'; 
	$output[] = '</table>'; 
	$output[] = '<table border="0" width="100%" align="center">';
	$output[] = '<tr class="estimatetitle">';
	$output[] = '<td colspan="2" nowrap>Item</td>';
	$output[] = '<td colspan="1" width="100px">Quantity</td>';
	$output[] = '<td colspan="1" width="100px">Cost</td>';
	$output[] = '</tr>';
	$output[] = '<tr>';
	$output[] = '<td colspan="4"><hr size="2"></td>';
	$output[] = '</tr>';
foreach ($contents as $id=>$qty) {
	$sql = 'SELECT * FROM product WHERE id = '.$id;
	$result = $db->query($sql);
	$row = $result->fetch();
	extract($row);

	$output[] = '<tr>';
	$output[] = '<td class="estimatedesc">'.$title.'</td>';
	$output[] = '<td><a href="estimate.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
	$output[] = '<td class="estimateqty"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
	$output[] = '<td class="estimatecost">£'.$salesprice.'</td>';
    $output[] = '</tr>';
	$output[] = '<input name="id[]" type="hidden" value='.$id. ' />'; 
	$subtotal += $salesprice * $qty;

	$output[] = '</table>';
	$output[] = '<tr>';
	$output[] = '<td colspan="4"><hr size="1"></td>';
	$output[] = '</tr>';
}
	$output[] = '<p align="right"><input type="text" name="labour'.$id.'" value="'.$labour.'" size="3" maxlength="3" /></p>';
	$total += $subtotal + $labour;
	$output[] = '<p align="right">Subtotal: <strong>£'.$subtotal.'</strong></p>';
	$output[] = '<p align="right">Total: <strong>£'.$total.'</strong></p>';
	$output[] = '<div><button type="submit">Update</button></div>';
	$output[] = '</form>';
} else {
	$output[] = '<p>Your shopping cart is empty.</p>';
}
return join('',$output);
}

 

I'm calling the function ShowCart on my cart page.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/133812-problem-holding-value-in-variable/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.