Jump to content

How to store checkbox array in a session, and then be able to add to it?


Scooby08

Recommended Posts

I'm trying to store an array of checkbox values into a session array, sort of like a shopping cart, but I can't figure out how to not overwrite the sessions array each time new checkbox values are added to the stored session...

 

Here is an array that is created upon submitting with two checkboxes checked:

 

Array
(
    [0] => 606613
    [1] => 606614
)

 

And then my sessions array is now like so:

 

Array
(
    [checked] => Array
        (
            [0] => 606613
            [1] => 606614
        )

)

 

The code for that so far is this:

 

<?php
if (!isset($_SESSION['checked'])) {
    $_SESSION['checked'] = array();
}
$_SESSION['checked'] = $_GET['new_checked'];
?>

 

I know I probably need to add a loop that checks the arrays, but I just can't think how I should go about adding newly checked checkbox values, that are not duplicates, to the *end* of the session array "checked"??

 

Would anybody care to share some ideas?

 

THANKS!

 

 

 

Nice! I think that's working perfectly! I added one thing to get rid of duplicates..

 

$_SESSION['checked'] = array_unique(array_values(array_merge($_SESSION['checked'], $_GET['new_checked'])));

 

That seems to work.. See any flaws with that?

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.