TheJoey Posted August 29, 2009 Share Posted August 29, 2009 im having trouble writting to file. Note its XML im trying to write to <form name="thisForm" method="POST"> <ul> <li>Something old <a href="#" onclick="addToCart(1)">Add To Cart</a> (<?php if(isset($mycart->items["1"])) print $mycart->items["1"]; ?>)</li> <li>Something new <a href="#" onclick="addToCart(2)">Add To Cart</a> (<?php if(isset($mycart->items["2"])) print $mycart->items["2"]; ?>)</li> <li>Something borrowed <a href="#" onclick="addToCart(3)">Add To Cart</a> (<?php if(isset($mycart->items["3"])) print $mycart->items["3"]; ?>)</li> <li>Something blue <a href="#" onclick="addToCart(4)">Add To Cart</a> (<?php if(isset($mycart->items["4"])) print $mycart->items["4"]; ?>)</li> </ul> <input type="text" name="itemId"> <input type="text" name="qty" value="1"> <input type="hidden" name="cart" value="<?=$mycart->xml();?>"> </form> rest of code <?php require_once("ShoppingCart.php"); $mycart = new Cart("<items/>"); if (isset($_POST["cart"])) $mycart = new Cart($_POST["cart"]); if (isset($_POST["itemId"])) { $id = $_POST["itemId"]; if (isset($mycart->items[$id])) $mycart->items[$id] += $_POST["qty"]; else $mycart->add_item($_POST["itemId"], $_POST["qty"]); } ?> <html> <head> <script language="JavaScript"> function addToCart(id) { document.thisForm.itemId.value = id; document.thisForm.submit(); } </script> </head> <body> <form name="thisForm" method="POST"> <ul> <li>Something old <a href="#" onclick="addToCart(1)">Add To Cart</a> (<?php if(isset($mycart->items["1"])) print $mycart->items["1"]; ?>)</li> <li>Something new <a href="#" onclick="addToCart(2)">Add To Cart</a> (<?php if(isset($mycart->items["2"])) print $mycart->items["2"]; ?>)</li> <li>Something borrowed <a href="#" onclick="addToCart(3)">Add To Cart</a> (<?php if(isset($mycart->items["3"])) print $mycart->items["3"]; ?>)</li> <li>Something blue <a href="#" onclick="addToCart(4)">Add To Cart</a> (<?php if(isset($mycart->items["4"])) print $mycart->items["4"]; ?>)</li> </ul> <input type="text" name="itemId"> <input type="text" name="qty" value="1"> <input type="hidden" name="cart" value="<?=$mycart->xml();?>"> </form> </body> </html> <?php class Cart { var $items; // Items collection var $parser; var $cur_id; var $bIsID; var $bIsQty; function Cart($items_xml) { $this->bIsID = false; $this->bIsQty = false; $this->parser = xml_parser_create(); xml_set_object(&$this->parser, &$this); xml_set_element_handler(&$this->parser, array(&$this, "tag_open"), array(&$this, "tag_close")); xml_set_character_data_handler(&$this->parser, array(&$this, "cdata")); xml_parse(&$this->parser, &$items_xml); unset($this->cur_item); unset($this->bIsID); unset($this->bIsQty); } function tag_open($pparser, $tag, $attrs) { if ($tag == "ID") $this->bIsID = true; if ($tag == "QTY") $this->bIsQty = true; } function tag_close($pparser, $tag) { if ($tag == "ID") $this->bIsID = false; if ($tag == "QTY") $this->bIsQty = false; } function cdata($pparser, $data) { if ($this->bIsID) $this->cur_id = $data; if ($this->bIsQty) $this->items[$this->cur_id] = $data; } function add_item($id, $qty) { $this->items[$id] = $qty; } function update_qty($id, $qty) { $this->add_item($id, $qty); } function remove_item($id) { unset($this->items[$id]); } function xml() { $s = "<items>"; foreach($this->items as $key => $value) { $s .= "<item><id>" . $key . "</id><qty>" . $value . "</qty></item>"; } $s .= "</items>"; return $s; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172372-writting-to-file-trouble/ Share on other sites More sharing options...
oni-kun Posted August 29, 2009 Share Posted August 29, 2009 what is exactly your trouble? You're not telling us any errors or problems that you're having, it isn't so helpful. Quote Link to comment https://forums.phpfreaks.com/topic/172372-writting-to-file-trouble/#findComment-908867 Share on other sites More sharing options...
TheJoey Posted August 29, 2009 Author Share Posted August 29, 2009 i dont know what to write to file. And how about to do it. im complety stuck on how to use this shopping cart, Quote Link to comment https://forums.phpfreaks.com/topic/172372-writting-to-file-trouble/#findComment-909017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.