This code block is easier to read than the same code without it:
<?php
class cart {
var $items;
function add_item($x) // $x is an array
{
$this->items[] = $x;
}
function remove_item($skew) // $skew is the sku number of item to be removed
{
for($i = 0; $i < count($this->items); $i++)
{
if($this->items[$i]['sku'] == $skew)
{
unset($this->items[$i]);
$this->items = array_values($this->items);
}
}
}
function dislpay_items()
{
$total = 0;
for($i = 0; $i < count($this->items); $i++)
{
echo "Item number: ".$i." sku: ".$this->items[$i]['sku']." Price: ".$this->items[$i]['price']." Qty: ".$this->items[$i]['qty']." subtotal: ".$this->items[$i]['price']*$this->items[$i]['qty']."<br>";
$total += $this->items[$i]['price'] * $this->items[$i]['qty'];
}
echo "Total: ".$total."<br>";
}
}
session_start();
//session_destroy();
if(isset($_SESSION['myclass']))
{
$mycart = $_SESSION['myclass'];
$mycart->remove_item(343);
} else
{
$mycart = new cart;
$mycart->add_item(array('sku'=>101, 'price'=>1.25 , 'qty'=>12));
$mycart->add_item(array('sku'=>343, 'price'=>99.95, 'qty'=>2));
$mycart->add_item(array('sku'=>233, 'price'=>4.99, 'qty'=>6));
}
$mycart->dislpay_items();
$_SESSION['myclass'] = $mycart;
?>
<?php
class cart {
var $items;
function add_item($x) // $x is an array
{
$this->items[] = $x;
}
function remove_item($skew) // $skew is the sku number of item to be removed
{
for($i = 0; $i < count($this->items); $i++)
{
if($this->items[$i]['sku'] == $skew)
{
unset($this->items[$i]);
$this->items = array_values($this->items);
}
}
}
function dislpay_items()
{
$total = 0;
for($i = 0; $i < count($this->items); $i++)
{
echo "Item number: ".$i." sku: ".$this->items[$i]['sku']." Price: ".$this->items[$i]['price']." Qty: ".$this->items[$i]['qty']." subtotal: ".$this->items[$i]['price']*$this->items[$i]['qty']."<br>";
$total += $this->items[$i]['price'] * $this->items[$i]['qty'];
}
echo "Total: ".$total."<br>";
}
}
session_start();
//session_destroy();
if(isset($_SESSION['myclass']))
{
$mycart = $_SESSION['myclass'];
$mycart->remove_item(343);
} else
{
$mycart = new cart;
$mycart->add_item(array('sku'=>101, 'price'=>1.25 , 'qty'=>12));
$mycart->add_item(array('sku'=>343, 'price'=>99.95, 'qty'=>2));
$mycart->add_item(array('sku'=>233, 'price'=>4.99, 'qty'=>6));
}
$mycart->dislpay_items();
$_SESSION['myclass'] = $mycart;
?>
If you want help you should do everything you can to make it easy for people who aren't paid to help you, to do that! That means clearly explaining your problem, your attempts to solve it, and posting the relevant portion of the code inside code blocks.
Following the rules makes it more likely that you will receive the help you need, so why not do it?
See image for code tag icon location and tags.
Attached image(s)
This post has been edited by CTphpnwb: 28 April 2010 - 05:02 PM

New Topic/Question
Reply




MultiQuote







|