-4

i received this error while loading this page. the error is like this, Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in /server/disk_1/websites/makemyrice/cart_update.php on line 7 the line number 7 is "if(isset($_COOKIE['cards']))"

 <?php


    class CartUpdate
    {

        if(isset($_COOKIE['cards']))
        {
        $cookie = $_COOKIE['cards'];
        $cookie = stripslashes($cookie);
        $savedCardArray = json_decode($cookie, true);
        $variety=$savedCardArray['CART'][0];
        $quantity=$savedCardArray['CART'][1];
        $bran=$savedCardArray['CART'][2];
        }
    setcookie('total', $total);
        }
        function __construct()
        {   

        }
        function add()
        {
            $variety = test_input($_POST["variety"]);  
            $rice_type = test_input($_POST["rice_type"]);
            $quantity = test_input($_POST["quantity"]);
            $bran = test_input($_POST["bran"]);
            $items[]=array($variety,$quantity,$bran,$rice_type);
            $json = json_encode($items);
            setcookie('cards', $json);
            print_r($items);
        }

        function delete()
        {
            setcookie( 'cards', "", time()-3600);
            header('location:cart.php');            
        }

        function cartlen()
        {

        }

    ?>

1 Answers1

2

You can not place the app logic inside your class like that. Classes have their rules.

If you are trying to call your codes when this class is initialized; you have to place them in the class constructor.

<?php

class CartUpdate
{

    /**
     * Initialize the Cart.
     * @return void
     */
    public function __construct() {

        if(isset($_COOKIE['cards'])) {
            $cookie = $_COOKIE['cards'];
            $cookie = stripslashes($cookie);
            $savedCardArray = json_decode($cookie, true);
            $variety=$savedCardArray['CART'][0];
            $quantity=$savedCardArray['CART'][1];
            $bran=$savedCardArray['CART'][2];
        }
        setcookie('total', $total);
    }

    }
    function add()
    {
        $variety = test_input($_POST["variety"]);  
        $rice_type = test_input($_POST["rice_type"]);
        $quantity = test_input($_POST["quantity"]);
        $bran = test_input($_POST["bran"]);
        $items[]=array($variety,$quantity,$bran,$rice_type);
        $json = json_encode($items);
        setcookie('cards', $json);
        print_r($items);
    }

    function delete()
    {
        setcookie( 'cards', "", time()-3600);
        header('location:cart.php');            
    }

    function cartlen()
    {

    }

?>
Hilmi Erdem KEREN
  • 1,949
  • 20
  • 29