Jump to content

PHP array session help


Morty222

Recommended Posts

A user will select a tournament to register for, then add a team, then I store the following into a session:

 

$teamID;

$tournyID;

$gatefee;

$cost;

 

When they click add to cart I update the session using this:

 

$_SESSION['cart'][]['team'] = $teamID;

$_SESSION['cart'][]['tournament'] = $tournyID;

$_SESSION['cart'][]['gatefee'] = $gatefee;

$_SESSION['cart'][]['cost'] = $cost;

 

My array comes out like this:

 

Array

(

[cart] => Array

(

[] => Array

(

[team] => 2137

[tournament] => 24490

[gatefee] =>

[cost] => 575

)

 

)

 

)

 

As you can see there is no KEY so when someone adds another tournament to their cart it overwrites this one. How can I get the KEY to number 0, 1, 2, etc... with each addition to the cart?

 

Thanks in advance!!!!

Link to comment
https://forums.phpfreaks.com/topic/127444-php-array-session-help/
Share on other sites

$_SESSION['cart'][]['team'] = $teamID;

 

[] is empty obviously...I just wrote pretty much the same code but I put a [$cartindex] that starts at 0 and increments for each new cart.  If you only want one cart then put 0 or 1 in a variable and code:

 

$_SESSION['cart'][$cartindex]['team'] = $teamID;

 

Array

(

[cart] => Array

(

[0] => Array(

[team] => 2137

[tournament] => 24490

[gatefee] =>

[cost] => 575

 

hope that helps a little.  I have a lot more code to support the cartindex and you should test the refresh for double post to your array.  when my post data is created I set the time created and when I check the post data I compare incoming time with my current cart and if they are equal then I don't create a new cart just the current cart index....

darkwater....

 

in your example, would by chance PHP increment [] starting at 0 and going forth from there?  I spent a lot of time setting my array starting at 0 and adding one etc myself and probably if I understood array processing better could have saved myself lots of work.  hope that is still on topic..

 

$_SESSION['cart'][] = array('team' => $teamID, 'tournament' => $tourneyID); //etc

darkwater....

 

in your example, would by chance PHP increment [] starting at 0 and going forth from there?  I spent a lot of time setting my array starting at 0 and adding one etc myself and probably if I understood array processing better could have saved myself lots of work.  hope that is still on topic..

 

$_SESSION['cart'][] = array('team' => $teamID, 'tournament' => $tourneyID); //etc

 

Indeed, it would increment it.  This code:

 

$array['cart'][] = array('team' => 15, 'tournament' => 5);
$array['cart'][] = array('team' => 12, 'tournament' => 7);
$array['cart'][] = array('team' => 76, 'tournament' => 2);
print_r($array);

 

Gives:

Array
(
    [cart] => Array
        (
            [0] => Array
                (
                    [team] => 15
                    [tournament] => 5
                )

            [1] => Array
                (
                    [team] => 12
                    [tournament] => 7
                )

            [2] => Array
                (
                    [team] => 76
                    [tournament] => 2
                )

        )

)

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.