Flic Posted February 5, 2007 Share Posted February 5, 2007 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: <?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: <?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! Link to comment https://forums.phpfreaks.com/topic/37132-adding-item-to-shopping-cart-from-array-data-not-carried-over/ Share on other sites More sharing options...
rantsh Posted February 5, 2007 Share Posted February 5, 2007 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? Link to comment https://forums.phpfreaks.com/topic/37132-adding-item-to-shopping-cart-from-array-data-not-carried-over/#findComment-177310 Share on other sites More sharing options...
Flic Posted February 5, 2007 Author Share Posted February 5, 2007 Sorry, didn't realise it had all gone onto one line! <?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>"; } ?> <?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. Link to comment https://forums.phpfreaks.com/topic/37132-adding-item-to-shopping-cart-from-array-data-not-carried-over/#findComment-177349 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.