Jump to content

Writting to File Trouble


TheJoey

Recommended Posts

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;
    }

}
?>

Link to comment
https://forums.phpfreaks.com/topic/172372-writting-to-file-trouble/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.