richrock Posted April 20, 2009 Share Posted April 20, 2009 Being relatively new to php, I've slowly been building my skills, and now I come to sessions.... I'm trying to write a comparison script, where people select images using checkboxes, and then at any point they can refer to the images side by side to compare them. I've been reading up on arrays, written a few in the script. But I've never done arrays where I can add another single item to it. I'm figuring I need to use array_push()? Also, I realised quite quickly that this needs to go in to a session. I set up a session, but all I can get it to do is write over my currently stored session. I then got really confused with tutorials because they always put defined arrays (eg $my_array = array('Dog','Cat','Mouse'); ) which is no good to me because I'm using dynamic data. Anyway here's my code so far: // Comparison bits $maker_sess_arr = array(); if (isset($_SESSION['testarray'])) { echo "<pre>"; print_r($_SESSION); echo "</pre>"; } if ($_POST['storecompare'] == 'on') { $_SESSION['testarray'] = $maker_sess_arr; } and the checkbox bit echo "<form action='' method='post' name='compare'>"; ?> <input type='checkbox' name='storecompare' onclick='this.form.submit();'/> Compare <input type='hidden' name='compareimage' value='<?php echo $thumbdir.$file; ?>' /> <input type='hidden' name='makername' value='<?php echo $surname." ".$firstname." ".$secondname." ".$thirdname." ".$fourthname." ".$fifthname; ?>' /> <input type='hidden' name='makeryear' value='<?php echo $yearmade; ?>' /> <input type='hidden' name='makerlocation' value='<?php echo ucfirst($location); ?>' /> <?php echo "</form>"; So how can I add the next image to the array? If I can figure that, then there is a second issue. I need to limit the array to 5 items (side by side images). I can't seem to find anything in the php documentation except count().... UPDATE - count()will work fine if I use it to check the amount in the array when it's called, before storing to the session again. So just how to store the next item into an existing array??? Link to comment https://forums.phpfreaks.com/topic/154913-two-problems-with-array-and-session/ Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 You can add to an array by either doing: $array[] = 'something'; // will add "something" on the end of the array Or: $array = array(); array_push($array, 'something'); // will do pretty much the same I prefer the second method as I find it neater, and it's supposedly faster as well. Good luck. Link to comment https://forums.phpfreaks.com/topic/154913-two-problems-with-array-and-session/#findComment-814823 Share on other sites More sharing options...
richrock Posted April 20, 2009 Author Share Posted April 20, 2009 Thanks, but can't seem to get it to work ??? if ($_POST['makername'] != NULL) { if (isset($_SESSION['testarray'])){ print_r($_SESSION['testarray']); $temp_test = array($_SESSION['testarray']); print_r($temp_test); } $arr_test_chk = count($temp_test); if ($arr_test_chk > 5) { echo "No more room at the inn"; } else { echo "Image stored for comparison."; $maker_sess_arr[] = $_POST['makername']; print_r($maker_sess_arr); } } actually just realised what I'm doing wrong - the $temp_test magically changes to $maker_sess_arr !!! The limitation using count() works too - next step is removing lines from arrays... Thanks. Link to comment https://forums.phpfreaks.com/topic/154913-two-problems-with-array-and-session/#findComment-814847 Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 Just wrote this - <?php session_start(); $array = (!isset($_SESSION[''])) ? $_SESSION[''] = array('a', 'b', 'c') : $_SESSION['']; if(!isset($_GET['array'])) { foreach($array as $key => $value) { echo $value.' - <a href="'.$_SERVER['PHP_SELF'].'?array='.$key.'">Delete</a><br />'; } } else if(isset($_GET['array'])) { $id = $_GET['array']; $array = $_SESSION['']; unset($array[$id]); $_SESSION[''] = $array; header('Location: '.$_SERVER['HTTP_REFERER']); } ?> Just a basic idea of how to delete session variables. Link to comment https://forums.phpfreaks.com/topic/154913-two-problems-with-array-and-session/#findComment-814959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.