Subject: Ch. 3 PHP example
Posted By: bccliff Post Date: 5/27/2008 10:54:32 AM
Hi:
Struggling with the code on pgs. 87-88 for the chapter 3 PHP version of the shopping cart.

My Catalogue1PHP.htm comes up just fine (server is WampServer) with the link to "Add to Shopping Cart" just fine, but upon a click nothing happens.

Couple of things:
1) Have downloaded the code from WROX and in Catalogue1PHP.htm I changed listed CSharp to C# (minor).
2) In CartPHP.js function AddRemove Item(action), the if statement in if(action == "Add") the two xHRObject.open's look exactly the same on both sides of the else (?) although in the book on page 81 there looks to be a space before on the else side of " managecart.php?action=......

3) Have checked the errata and don't see and listing for this coding section

Wondering if there has been any posting re: typos/fixes
I now sound like my students but my code looks good! Sorry

Any suggestions would be helpful as I would like to use this as my chapter 3 assignment.

TNX

Cliff

Reply By: Chrisull Reply Date: 6/4/2008 7:12:32 AM
Sorry there's two mistakes. You're correct with point 2) that it's a tautology. Just remove the if then and the else condition. It shouldn't have any bearing on the code (apart from making it non-optimal).

Second the PHP is incorrect. I'm not sure if it's the book or site code that is wrong. I submitted an updated version of the code I believe way back to the site, but this may not be on there. It should read as follows:

<?php
session_register('Cart');
header('Content-Type: text/xml');
?>
<?php
        $newitem = $_GET["book"];
        $action = $_GET["action"];
        if ($_SESSION["Cart"] != "")
        {
            $MDA = $_SESSION["Cart"];
            if ($action == "Add")
            {
                if ($MDA[$newitem] != "")
                {  
                    $value = $MDA[$newitem] + 1;
                    $MDA[$newitem] = $value;
                    $_SESSION["Cart"] = $MDA;
                    ECHO (toXml($MDA));    
                }
                else
                {
                    $MDA[$newitem] = "";
                    $_SESSION["Cart"] = $MDA;
                    ECHO (toXml($MDA));
                }
            }
            else
            {
                $MDA= "";
                $_SESSION["Cart"] = "";   
                ECHO (toXml($MDA));
            }
        }
        else
        {
            $MDA[$newitem] = "1";
            $_SESSION["Cart"] = $MDA;
            ECHO (toXml($MDA));
        }
                                        
    
    function toXml($MDA)
    {
        $doc = new DomDocument('1.0');
        $cart = $doc->createElement('cart');
        $cart = $doc->appendChild($cart);
        
        foreach ($MDA as $Item => $ItemName)
        {
        
        $book = $doc->createElement('book');
        $book = $cart->appendChild($book);

        $title = $doc->createElement('title');
        $title = $book->appendChild($title);   
        $value = $doc->createTextNode($Item);
        $value = $title->appendChild($value);

        $quantity = $doc->createElement('quantity');
        $quantity = $book->appendChild($quantity);
        $value2 = $doc->createTextNode($ItemName);
        $value2 = $quantity->appendChild($value2);
     
      }

        $strXml = $doc->saveXML();
        return $strXml;
    }
?>

Let me know if that helps,
Chris



Chris Ullman
Programmer/Technical Author
http://www.cuasp.co.uk

Go to topic 71419

Return to index page 2
Return to index page 1