Reply LinkBack Thread Tools Search this Thread
Old 21-04-2007, 03:10   #1 (permalink)
Aen
i love MUJI
 
Aen's Avatar
 
Join Date: Mar 2007
Location: Psedo Tokyo
Posts: 158
Send a message via AIM to Aen Send a message via MSN to Aen Send a message via Skype™ to Aen
Need help with converting PHP5 code to PHP4

I'm not a programmer but a job at hand requires me to build something like a shopping cart with Ajax so I read some tutorials and got my hands dirty with Prototype.

Nothing wrong with the JS but the PHP that is returning the results seems to be written in PHP5 so it doesn't work on my server (which has PHP4).

PHP Code:
<?php
session_start
();

class 
Shopping_Cart {

    private 
$cart;

    function 
__construct($cart="") {
        
$this->cart $cart;
    }

    function 
getCart() {
        return 
$this->cart;
    }
   
    function 
addToCart($item) {
        if(isset(
$this->cart[$item])) {
            
$this->cart[$item]++;
        } else {
            
$this->cart[$item] = 1;
        }      
    }

    function 
deleteFromCart($item) {
        if(isset(
$this->cart[$item])) {
            
$this->cart[$item]--;
            if(
$this->cart[$item] == 0) {
                unset(
$this->cart[$item]);
            }
        }
    }
   
}

$items $_SESSION["cart"];

// Retrieve the parameters
$task $_GET['task'];
$item $_GET['item'];

if (
$task == "add") {

   
$cart->addToCart($item);
   
$_SESSION["cart"] = $cart->getCart();
   echo 
"item added!";
   
} else {

   
$cart->deleteFromCart($item);
   echo 
"item deleted!";
   
}
?>

The error displayed:

Quote:
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /pathtomyscript/managecart.php on line 6

Line 6 is where it says private $cart

I hope someone can be kind enough to show me how to turn the class into PHP4 code.
  Reply With Quote
Old 21-04-2007, 03:34   #2 (permalink)
WeeJames
Dog Fighter
 
WeeJames's Avatar
 
Join Date: Oct 2006
Location: Glasgow.
Posts: 50
Change that line to
Code:
var $cart;
  Reply With Quote
Old 21-04-2007, 08:24   #3 (permalink)
freelancr
Senior Member
 
freelancr's Avatar
 
Join Date: Oct 2006
Posts: 2,175
From what i've heard about the differences between php4 and php5 is that php5 is more object orientated, so the whole lot may need rewriting or you could move to a different host that supports php5.
  Reply With Quote
Old 21-04-2007, 10:22   #4 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 229
Send a message via AIM to Cborrow
You will probaly also need to change

PHP Code:
function __construct 

to

PHP Code:
function Shopping_Cart 
  Reply With Quote
Old 21-04-2007, 13:08   #5 (permalink)
Aen
i love MUJI
 
Aen's Avatar
 
Join Date: Mar 2007
Location: Psedo Tokyo
Posts: 158
Send a message via AIM to Aen Send a message via MSN to Aen Send a message via Skype™ to Aen
Thanks guys,

changing private to var and using the class name instead of __construct did the trick.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8