Jump to content

Shopping cart that uses xml or .txt


TheJoey

Recommended Posts

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.

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"));

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

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"));

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

* 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

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.