TheJoey Posted August 27, 2009 Share Posted August 27, 2009 could anyone point me at a tutiroul please. ive searched everywere. Quote Link to comment Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 ive searched everywere. first result on Google with an obvious query: Creating an XML-based shopping cart in PHP: http://articles.techrepublic.com.com/5100-10878_11-5662732.html Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 27, 2009 Author Share Posted August 27, 2009 i had found that website, but didnt see the links that were inside the body. thanks Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 27, 2009 Author Share Posted August 27, 2009 Having major issues getting this script to work home.php <?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> (<? if(isset($mycart->items["1"])) print $mycart->items["1"]; ?>)</li> <li>Something new <a href="#" onclick="addToCart(2)">Add To Cart</a> (<? if(isset($mycart->items["2"])) print $mycart->items["2"]; ?>)</li> <li>Something borrowed <a href="#" onclick="addToCart(3)">Add To Cart</a> (<? if(isset($mycart ->items["3"])) print $mycart->items["3"]; ?>)</li> <li>Something blue <a href="#" onclick="addToCart(4)">Add To Cart</a> (<? if(isset($mycart->items["4"])) print $mycart->items["4"]; ?>)</li> </ul> <input type="hidden" name="itemId"> <input type="hidden" name="qty" value="1"> <input type="hidden" name="cart" value="<?=$mycart->xml();?>"> </form> </body> </html> ShoppingCart.php <?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, "tag_open", "tag_close"); xml_set_character_data_handler(&$this->parser, "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; } } ?> * Something old Add To Cart (items["1"])) print $mycart->items["1"]; ?>) * Something new Add To Cart (items["2"])) print $mycart->items["2"]; ?>) * Something borrowed Add To Cart (items["3"])) print $mycart->items["3"]; ?>) * Something blue Add To Cart (items["4"])) print $mycart->items["4"]; ?>) im getting that when i try to run it. Quote Link to comment Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 xml_set_element_handler(&$this->parser, "tag_open", "tag_close"); should be: xml_set_element_handler(&$this->parser, array(&$this, "tag_open"), array(&$this, "tag_close")); same for: xml_set_character_data_handler(&$this->parser, array(&$this, "cdata")); Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted August 27, 2009 Share Posted August 27, 2009 Before you continue with your xml shopping cart or txt based shopping cart. Is the reason perhaps because you can't use a database such as MySQL or postgres? You might want to look at flatfile databases such as SQLite Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 27, 2009 Author Share Posted August 27, 2009 well its just personal preferences i guess, and my hosting charge for database. Quote Link to comment Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 well its just personal preferences i guess, and my hosting charge for database. They charge extra for a database? Damn find yourself a real host. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted August 27, 2009 Share Posted August 27, 2009 I mentioned a flatfile database because it's just a file on your server. You shouldn't be charged extra for using a flatfile database if you're server supports it. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 27, 2009 Author Share Posted August 27, 2009 Yeh my provider are rubbish but ive got it now.. Thanks for the idea dj kat but ive already used some xml in my website already ive baised the website around using .txt files. im currently getting this website And thanks for the quick reply's guys Parse error: syntax error, unexpected ',', expecting T_PAAMAYIM_NEKUDOTAYIM in C:\xampplite\htdocs\Shopping Cart xml\ShoppingCart.php on line 17 For this code xml_set_character_data_handler(&$this->parser, array(&this, "cdata")); Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 27, 2009 Author Share Posted August 27, 2009 Ignore the above post i was missing the $ * Something old Add To Cart (items["1"])) print $mycart->items["1"]; ?>) * Something new Add To Cart (items["2"])) print $mycart->items["2"]; ?>) * Something borrowed Add To Cart (items["3"])) print $mycart->items["3"]; ?>) * Something blue Add To Cart (items["4"])) print $mycart->items["4"]; ?>) I think this is what is meant to be display but when i try clicking on the links i get nothing <?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; } } ?> <?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> (<? if(isset($mycart->items["1"])) print $mycart->items["1"]; ?>)</li> <li>Something new <a href="#" onclick="addToCart(2)">Add To Cart</a> (<? if(isset($mycart->items["2"])) print $mycart->items["2"]; ?>)</li> <li>Something borrowed <a href="#" onclick="addToCart(3)">Add To Cart</a> (<? if(isset($mycart ->items["3"])) print $mycart->items["3"]; ?>)</li> <li>Something blue <a href="#" onclick="addToCart(4)">Add To Cart</a> (<? if(isset($mycart->items["4"])) print $mycart->items["4"]; ?>)</li> </ul> <input type="hidden" name="itemId"> <input type="hidden" name="qty" value="1"> <input type="hidden" name="cart" value="<?=$mycart->xml();?>"> </form> </body> </html> Home.php im still getting this error after fixing it Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 28, 2009 Author Share Posted August 28, 2009 It doesnt seem to be storing to a file or anything. I dont know what im meant to do. Quote Link to comment Share on other sites More sharing options...
trq Posted August 28, 2009 Share Posted August 28, 2009 Yeh my provider are rubbish You get what you pay for, obviously your not willing to pay for a decent host. Anyway, What error are you getting now? Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 28, 2009 Author Share Posted August 28, 2009 * Something old Add To Cart (items["1"])) print $mycart->items["1"]; ?>) * Something new Add To Cart (items["2"])) print $mycart->items["2"]; ?>) * Something borrowed Add To Cart (items["3"])) print $mycart->items["3"]; ?>) * Something blue Add To Cart (items["4"])) print $mycart->items["4"]; ?>) that is showing up when i run the code Where "Add to cart" is a link but it doesnt do anything. <?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; } } ?> Shoppingcart.php <?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> (<? if(isset($mycart->items["1"])) print $mycart->items["1"]; ?>)</li> <li>Something new <a href="#" onclick="addToCart(2)">Add To Cart</a> (<? if(isset($mycart->items["2"])) print $mycart->items["2"]; ?>)</li> <li>Something borrowed <a href="#" onclick="addToCart(3)">Add To Cart</a> (<? if(isset($mycart->items["3"])) print $mycart->items["3"]; ?>)</li> <li>Something blue <a href="#" onclick="addToCart(4)">Add To Cart</a> (<? if(isset($mycart->items["4"])) print $mycart->items["4"]; ?>)</li> </ul> <input type="hidden" name="itemId"> <input type="text" name="qty" value="1"> <input type="hidden" name="cart" value="<?=$mycart->xml();?>"> </form> </body> </html> home.php Quote Link to comment Share on other sites More sharing options...
trq Posted August 28, 2009 Share Posted August 28, 2009 Use <?php tags not <? Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 28, 2009 Author Share Posted August 28, 2009 Ok thanks for that. Its now adding 1 to each item, how would i store this information for later use? For like a checkout or order form. would i need to use a session start Quote Link to comment Share on other sites More sharing options...
trq Posted August 28, 2009 Share Posted August 28, 2009 how would i store this information for later use? You need to write the xml to an xml file. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 28, 2009 Author Share Posted August 28, 2009 Can i do this threw fwrite or does it require something different? do you have a tut? Quote Link to comment Share on other sites More sharing options...
trq Posted August 28, 2009 Share Posted August 28, 2009 You can simply do it through fwrite or file_put_contents. Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 28, 2009 Author Share Posted August 28, 2009 ok ill have a play around with it ill keep you posted thanks for your help Quote Link to comment Share on other sites More sharing options...
TheJoey Posted August 28, 2009 Author Share Posted August 28, 2009 Is there a way where i can do on click add to file? Quote Link to comment 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.