0

I am just trying to write a function in php that adds to the discount array but it doesn't seem to work at all.

    function addToDiscountArray($item){
        // if we already have the discount array set up
        if(isset($_SESSION["discountCalculator"])){

            // check that this item is not already in the array
            if(!(in_array($item,$_SESSION["discountCalculator"]))){
                // add it to the array if it isn't already present
                array_push($_SESSION["discountCalculator"], $item);
            }
        }
        else{

            $array = array($item);
            // if the array hasn't been set up, initialise it and add $item
            $_SESSION["discountCalculator"] = $array;
        }
}

Every time I refresh the page it acts like $_SESSION["discountCalculator"] hasn't been set up but I can't understand why. Whilst writing can I use the $_SESSION["discountCalculator"] in a foreach php loop in the normal way too?

Many thanks

2
  • 4
    did you do a session_start() before doing anything? $_SESSION is always present, but it'll only be populated with stored values after you do session_start() Commented Apr 6, 2011 at 16:30
  • Thank you so much, I wrote the header script initially and had session_start() in there so assumed it would still be, but now since you mentioned it I've just gone to check it and it seems one of the people I'm building the website with has taken it out when he was adjusting the template! Thanks Commented Apr 6, 2011 at 16:35

1 Answer 1

1

The fact that every time $_SESSION['discountCalculator'] seems not to be set, could be because $_SESSION is not set (NULL). This case happens mostly when you did not executed session_start() at the beginning of you page. Try adding session_start() at the beginning of the function.

function addToDiscountArray($item) {
    if (!$_SESSION) { // $_SESSION is NULL if session is not started
        session_start(); // we need to start the session to populate it
    }
    // if we already have the discount array set up
    if(isset($_SESSION["discountCalculator"])){

        // check that this item is not already in the array
        if(!(in_array($item,$_SESSION["discountCalculator"]))){
            // add it to the array if it isn't already present
            array_push($_SESSION["discountCalculator"], $item);
        }
    }
    else{

        $array = array($item);
        // if the array hasn't been set up, initialise it and add $item
        $_SESSION["discountCalculator"] = $array;
    }
}

Notice that this will not affect the function if the session is already started. It will only run ´session_start()` if the session is not started.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your input, I feel like an idiot because I originally had this in the header script, but as I explained above, it seems one of my collaborators has removed it at some point... it's back where it belongs now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.