Jump to content

array as session variable on click to some image

Go to solution Solved by 0xMatt,

Recommended Posts

when i click on image1 with id=1, it stores in session and when i click on image2 with id=2, it stores in session too next to the first one, and when i click on image3 with id=3, it stores in session next to id=2. and so on....


i hope you understand what i am asking ....

need help.

i know how to get value from array....


<?php// begin the sessionsession_start(); // create an array$my_array=array('cat', 'dog', 'mouse', 'bird', 'crocodile', 'wombat', 'koala', 'kangaroo'); // put the array in a session variable$_SESSION['animals']=$my_array; // a little message to say we have done itecho 'Putting array into a session variable';// loop through the session array with foreachforeach($_SESSION['animals'] as $key=>$value)    {    // and print out the values    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';    }?>



but wat i want is, cat ,dog,mouse,bird,crocodile,wombat,koala,kangaroo ... stored in array when i click on them....


i hope you understand..


please help!!!


thanks and regards,

jazz.. :)

Link to comment
Share on other sites

  • Solution

<?phpsession_start(); $animals = array('cat', 'dog', 'mouse', 'bird', 'crocodile', 'wombat', 'koala', 'kangaroo'); if(isset($_GET['add_animal'])){$_SESSION['animals'][$animals[$_GET['add_animal']]] = 1;} echo '<h1>Animals to choose from</h1>'; foreach($animals as $key=> $value){// and print out the valuesecho "<a href='test2.php?add_animal={$key}'>{$value}</a></br>";} echo '<br/><br/>'; $added_animals = '';foreach($_SESSION['animals'] as $animal => $value){$added_animals .= $animal. ',';} echo 'The animal you have added are: ' . $added_animals; 

Try that.  Obviously change test2.php t your file name before you try it.
Edited by 0xMatt
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.