I'm a Flash developer by trade and use OOP on a daily basis so understand how things should work.
I'm hoping my problem is just syntactical.
Anyway, I'm building the simplest shopping cart, all it has to do is store the products temporarily, they will be passed out to Nochex's payment completion system, then dumped.
So, I have my page with an add to cart button. It kicks off with creating basket_arr, then on _POST creating an instance of itemVO, which in tern is passed to an instance of basket
<?php
session_start ( ) ;
if ( isset ( $_SESSION['basket_arr'] ) ) {
print 'basket exists ' . $_SESSION['basket_arr'] ;
} else {
$_SESSION['basket_arr'] = array ( ) ;
}
if(isset($_POST['add'])) {
$id = $_POST['id'] ;
$price = $_POST['price'] ;
$number = $_POST['number'] ;
require ( 'vo/itemVO.php' ) ;
$item = new itemVO ( ) ;
$item -> id = $id ;
$item -> price = $price ;
$item -> number = $number ;
require ( 'basket.php' ) ;
$basket = new basket ( ) ;
$basket -> addItem ( $item ) ;
header("Location: " . $_SERVER['PHP_SELF']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="post">
<p>Carrots - £6.99 -
<input name="id" type="hidden" value="carrots" />
<input name="price" type="hidden" value="1.50" />
<input name="number" type="text" value="1" size="3" maxlength="3" />
<input name="add" type="submit" class="box" id="carrots" value="Add To Basket"> </p>
</form>
</body>
</html>
I have my itemVO, simple enough:
<?php
class itemVO {
var $id ;
var $price ;
var $number ;
function UserVO() {
}
}
?>
Now the area that I'm having my trouble with is my basket.php, the plan is to have my session hold an array of itemVO's. Obviously if I submit the same item more than once I need to check that it exists, if it does, add it to the existing one if not add the new itemVO to the array.
<?php
class basket {
function addItem ( $item ) {
$len = count ( $_SESSION['basket_arr'] ) ;
if ( $len > 0 ) {
for ( $i = 0 ; $i < $len ; $i++ ) {
//loop through basket array
if ( $_SESSION['basket_arr'][$i]->id == $item->id ) {
//if the id of the session array is the same as the submitted item, add it
echo ( 'the item trying to be added already exists' ) ;
$_SESSION['basket_arr'][$i]->number += $item->number ;
} else {
echo ( 'there is no match add the new itemVO' ) ;
array_merge ( $_SESSION['basket_arr'] , $itemVO ) ;
}
}
} else {
//basket_arr has no length so go ahead and add it
$_SESSION['basket_arr'][0] = $item ;
}
}
}
?>
I hope this doesn't come across as another, "why doesn't this work" post, and more of a, "I know this should be working, any pointers as to why is might not be".
I know that I could navigate to basket.php passing the itemVO as a query and add it that way, but think I'll probably just have the same problem. Ultimately I want to be able to add items from the product page without navigating away.
Thanks for all your help in advance.
Jolyon