Author Topic: Adding item to shopping cart from array, data not carried over  (Read 274 times)

0 Members and 1 Guest are viewing this topic.

Offline FlicTopic starter

  • Irregular
    • View Profile
Adding item to shopping cart from array, data not carried over
« on: February 05, 2007, 06:58:02 AM »
Hi, I have some shopping basket code which works in that the item gets added to the shopping basket, but the information (name, price etc) isn't carried over.

I've only recently started learning php so am not sure why it doesn't work properly and would really appreciate it if someone could take a look and let me know where I am going wrong. It did work before but I heavily modified it for my needs hence it not working now . ???

Code that its being called from:
Code: [Select]
<?php$products = array();$products[1] = array("id"=>123,"name"=>"Silo Venting Filter","price"=>123.45,"sub"=>"venting_system", "desc"=>"A cylindrically shaped dust collector for venting of pneumatically filled silos, the stainless steel body contains vertically mounted filter elements. The air jet cleaning system is integrated in the hinged weather protection cover.");foreach($products as $p) {echo "<div class='acc'>";echo "<form method='post' action='cart.php'>";echo "<input type='hidden' name='id' value='".$p['id']."'/>";echo "<img src='images/".$p['sub'].".jpg' alt='".$p['name']."'>";echo "<div><h1><a class='silolink' href='accessories/".$p['sub'].".html'>".$p['name']."</a></h1>";echo $p['desc'];echo "<br><br>£".$p['price'];echo "<input type='submit' value='Add to cart' name='add'></form>";echo "</div></div>";}?>

The cart page:
Code: [Select]
<?phpsession_start();$cart =& $_SESSION['cart']; // point $cart to session cart.if(!is_object($cart)) $cart = new siloCart(); // if $cart ( $_SESSION['cart'] ) isn't an object, make a new cartclass siloCart { var $total = 0; var $itemcount = 0; var $items = array();        var $itemprices = array(); var $itemqtys = array();        var $iteminfo = array(); function cart() {} // constructor function function get_contents() { // gets cart contents $items = array(); foreach($this->items as $tmp_item) {         $item = FALSE; $item['id'] = $tmp_item;                        $item['name'] = $this->itemname[$tmp_item]; $item['price'] = $this->itemprices[$tmp_item]; $item['sub'] = $this->itemsub[$tmp_item]; $item['desc'] = $this->itemdesc[$tmp_item]; $item['subtotal'] = $item['qty'] * $item['price'];                        $items[] = $item; } return $items; } // end of get_contents function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE) { // adds an item to cart if($this->items[$itemid] > 0)                { // the item is already in the cart..   // so we'll just increase the quantity $this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid]; $this->_update_total(); } else { $this->items[]=$itemid; $this->itemqtys[$itemid] = $qty; $this->itemprices[$itemid] = $price; $this->iteminfo[$itemid] = $info; } $this->_update_total(); } // end of add_item function edit_item($itemid,$qty) { // changes an items quantity if($qty < 1) { $this->del_item($itemid); } else { $this->itemqtys[$itemid] = $qty; } $this->_update_total(); } // end of edit_item function del_item($itemid) { // removes an item from cart $ti = array(); $this->itemqtys[$itemid] = 0; foreach($this->items as $item) { if($item != $itemid) { $ti[] = $item; } } $this->items = $ti; $this->_update_total(); } //end of del_item        function empty_cart() { // empties / resets the cart                $this->total = 0;         $this->itemcount = 0;         $this->items = array();                $this->itemprices = array();         $this->itemqtys = array();                $this->itemdescs = array(); } // end of empty cart function _update_total() { // internal function to update the total in the cart         $this->itemcount = 0; $this->total = 0;                if(sizeof($this->items > 0)) {                        foreach($this->items as $item) {                                $this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]); $this->itemcount++; } } } // end of update_total}?><html here><?phpif($_POST['add']) { $product = $products[$_POST['id']]; $cart->add_item($product['id'],$product['price'],$product['name']);}if($_POST['remove']) { $rid = intval($_POST['id']); $cart->del_item($rid);}     echo "<table><tr><td>ID</td>";     echo "<td>Name</td>";     echo "<td>Price</td>";     echo "<td>Quan</td>";     echo "<td>Subtotal</td></tr>";if($cart->itemcount > 0) { foreach($cart->get_contents() as $item) {   echo "<tr><td>".$item['id']."</td>"; echo "<td>".$item['info']."</td>"; echo "<td>".number_format($item['price'],2)."</td>"; echo "<td>".$item['qty']."</td>";                echo "<td>".number_format($item['subtotal'],2)."</td>";                echo "<td><form method=post><input type='hidden' name='id' value='".$item['id']."'/><input type='submit' name='remove' value='X'/></form></td></tr>"; } echo "<tr><td colspan=4>Sub total:</td><td>£".number_format($cart->total,2)."</td></tr>"; echo "<tr><td colspan=4>VAT:</td><td>£".number_format($cart->total,2)."</td></tr>"; echo "<tr><td colspan=4>Total:</td><td>£".number_format($cart->total,2)."</td></tr>"; echo "</table>"; } else {               echo "<tr><td colspan=5>- No items found in cart -</td></tr>";         echo "</table>";}?></div></div></body></html>
Any help would be much appreciated!

Thanks!

Offline rantsh

  • Enthusiast
    • View Profile
    • Tecnologia para todos
Re: Adding item to shopping cart from array, data not carried over
« Reply #1 on: February 05, 2007, 07:04:07 AM »
I have a question and a request in order to read your code...

why do you have the <?php sticked together with the first function????

Could you please post your code in multiple lines so it'll be easier to read?
Roderick Smith.
Vivophone.com.
Get a free VIVOphone account with 30 prepaid minutes to call anywhere in the US (or some minutes to other places) by going to http://www.vivophone.com/vivosoft/index.php?promo=SR0979

Offline FlicTopic starter

  • Irregular
    • View Profile
Re: Adding item to shopping cart from array, data not carried over
« Reply #2 on: February 05, 2007, 08:24:07 AM »
Sorry, didn't realise it had all gone onto one line!

Code: [Select]
<?php
$products 
= array();
$products[1] = array("id"=>123,"name"=>"Silo Venting Filter","price"=>123.45,"sub"=>"venting_system""desc"=>"A cylindrically shaped dust collector for venting of pneumatically filled silos, the stainless steel body contains vertically mounted filter elements. The air jet cleaning system is integrated in the hinged weather protection cover.");

foreach(
$products as $p) {
echo 
"<div class='acc'>";
echo 
"<form method='post' action='cart.php'>";
echo 
"<input type='hidden' name='id' value='".$p['id']."'/>";
echo 
"<img src='images/".$p['sub'].".jpg' alt='".$p['name']."'>";
echo 
"<div><h1><a class='silolink' href='accessories/".$p['sub'].".html'>".$p['name']."</a></h1>";
echo 
$p['desc'];
echo 
"<br><br>£".$p['price'];
echo 
"<input type='submit' value='Add to cart' name='add'></form>";
echo 
"</div></div>";
}
?>

Code: [Select]
<?php
session_start
();
$cart =& $_SESSION['cart']; // point $cart to session cart.
if(!is_object($cart)) $cart = new siloCart(); // if $cart ( $_SESSION['cart'] ) isn't an object, make a new cart

class siloCart {
var $total 0;
var $itemcount 0;
var $items = array();
        var 
$itemprices = array();
var $itemqtys = array();
        var 
$iteminfo = array();

function cart() {} // constructor function

function get_contents()
// gets cart contents
$items = array();
foreach($this->items as $tmp_item)
{
        $item FALSE;

$item['id'] = $tmp_item;
                        
$item['name'] = $this->itemname[$tmp_item];
$item['price'] = $this->itemprices[$tmp_item];
$item['sub'] = $this->itemsub[$tmp_item];
$item['desc'] = $this->itemdesc[$tmp_item];
$item['subtotal'] = $item['qty'] * $item['price'];
                        
$items[] = $item;
}
return $items;
// end of get_contents


function add_item($itemid,$qty=1,$price FALSE$info FALSE)
// adds an item to cart
if($this->items[$itemid] > 0)
                { 
// the item is already in the cart..
  // so we'll just increase the quantity
$this->itemqtys[$itemid] = $qty $this->itemqtys[$itemid];
$this->_update_total();
} else {
$this->items[]=$itemid;
$this->itemqtys[$itemid] = $qty;
$this->itemprices[$itemid] = $price;
$this->iteminfo[$itemid] = $info;
}
$this->_update_total();
// end of add_item


function edit_item($itemid,$qty)
// changes an items quantity

if($qty 1) {
$this->del_item($itemid);
} else {
$this->itemqtys[$itemid] = $qty;
}
$this->_update_total();
// end of edit_item


function del_item($itemid)
// removes an item from cart
$ti = array();
$this->itemqtys[$itemid] = 0;
foreach($this->items as $item)
{
if($item != $itemid)
{
$ti[] = $item;
}
}
$this->items $ti;
$this->_update_total();
//end of del_item


        
function empty_cart()
// empties / resets the cart
                
$this->total 0;
        $this->itemcount 0;
        $this->items = array();
                
$this->itemprices = array();
        $this->itemqtys = array();
                
$this->itemdescs = array();
// end of empty cart


function _update_total()
// internal function to update the total in the cart
        $this->itemcount 0;
$this->total 0;
                if(
sizeof($this->items 0))
{
                        foreach(
$this->items as $item) {
                                
$this->total $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]);
$this->itemcount++;
}
}
// end of update_total

}
?>


<html links>

<?php

if($_POST['add']) {
$product $products[$_POST['id']];
$cart->add_item($product['id'],$product['price'],$product['name']);
}
if(
$_POST['remove']) {
$rid intval($_POST['id']);
$cart->del_item($rid);
}

    echo "<table><tr><td>ID</td>";
    echo "<td>Name</td>";
    echo "<td>Price</td>";
    echo "<td>Quan</td>";
    echo "<td>Subtotal</td></tr>";

if(
$cart->itemcount 0) {
foreach($cart->get_contents() as $item) {
  
echo "<tr><td>".$item['id']."</td>";
echo "<td>".$item['info']."</td>";
echo "<td>".number_format($item['price'],2)."</td>";
echo "<td>".$item['qty']."</td>";
                echo 
"<td>".number_format($item['subtotal'],2)."</td>";
                echo 
"<td><form method=post><input type='hidden' name='id' value='".$item['id']."'/><input type='submit' name='remove' value='X'/></form></td></tr>";
}
echo "<tr><td colspan=4>Sub total:</td><td>£".number_format($cart->total,2)."</td></tr>";
echo "<tr><td colspan=4>VAT:</td><td>£".number_format($cart->total,2)."</td></tr>";
echo "<tr><td colspan=4>Total:</td><td>£".number_format($cart->total,2)."</td></tr>";
echo "</table>";
} else {
       
       echo "<tr><td colspan=5>- No items found in cart -</td></tr>";
 
       echo "</table>";
}

?>

</div></div></body>
</html>

That looks like its gone on ok this time, sorry about that.

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.