richrock Posted April 21, 2009 Share Posted April 21, 2009 Hi, I've created some arrays to be stored in a session. They add to the session fine, I've introduced a limit to 5 (using if(count($sess_maker) > 4 ){}). This alerts people that they can't add any more items. However, when I have 4 or 5 items in the array, and click to remove one, it then removes two. If it's 3 items or less, it just removes one. I can't for the life figure out why (maybe its 8 hours sleep since thursday ) Here's the unset part of the code, that removes the item: if ($_POST['remove']) { if ($_POST['remove'] == 'nothing_a') { unset($sess_maker[0]); unset($sess_location[0]); unset($sess_year[0]); unset($sess_image[0]); unset($sess_type[0]); } if ($_POST['remove'] == 'nothing_b') { unset($sess_maker[1]); unset($sess_location[1]); unset($sess_year[1]); unset($sess_image[1]); unset($sess_type[1]); } if ($_POST['remove'] == 'nothing_c') { unset($sess_maker[2]); unset($sess_location[2]); unset($sess_year[2]); unset($sess_image[2]); unset($sess_type[2]); } if ($_POST['remove'] == 'nothing_d') { unset($sess_maker[3]); unset($sess_location[3]); unset($sess_year[3]); unset($sess_image[3]); unset($sess_type[3]); } if ($_POST['remove'] == 'nothing_e') { unset($sess_maker[4]); unset($sess_location[4]); unset($sess_year[4]); unset($sess_image[4]); unset($sess_type[4]); } $sess_maker = array_merge($sess_maker); $sess_location = array_merge($sess_location); $sess_year = array_merge($sess_year); $sess_image = array_merge($sess_image); $sess_type = array_merge($sess_type); array_pop($sess_maker); array_pop($sess_location); array_pop($sess_year); array_pop($sess_image); array_pop($sess_type); $_SESSION['sess_maker'] = $sess_maker; $_SESSION['sess_location'] = $sess_location; $_SESSION['sess_year'] = $sess_year; $_SESSION['sess_image'] = $sess_image; $_SESSION['sess_type'] = $sess_type; } I'm not 100% sure I need the array_merge(), but it seems to work fine. Any ideas what's going on here? Because I've got this feature fixed to five items, I didn't really bother with loops, so it should be straightforward... :'( Quote Link to comment https://forums.phpfreaks.com/topic/155038-whats-going-on-with-my-session-arrays/ Share on other sites More sharing options...
DeanWhitehouse Posted April 21, 2009 Share Posted April 21, 2009 try using elseifs' instead of just if e.g. if ($_POST['remove'] == 'nothing_a') { unset($sess_maker[0]); unset($sess_location[0]); unset($sess_year[0]); unset($sess_image[0]); unset($sess_type[0]); } elseif() { .... } Quote Link to comment https://forums.phpfreaks.com/topic/155038-whats-going-on-with-my-session-arrays/#findComment-815447 Share on other sites More sharing options...
kenrbnsn Posted April 21, 2009 Share Posted April 21, 2009 I would replace the series of "if" statements with: <?php $removes = array('nothing_a','nothing_b','nothing_c','nothing_d','nothing_d'); if (isset($_POST['remove']) && in_array($_POST['remove'],$removes)) { $key = array_search($_POST['remove'],$removes); unset($sess_maker[$key]); unset($sess_location[$key]); unset($sess_year[$key]); unset($sess_image[$key]); unset($sess_type[$key]); } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/155038-whats-going-on-with-my-session-arrays/#findComment-815460 Share on other sites More sharing options...
richrock Posted April 21, 2009 Author Share Posted April 21, 2009 Cheers guys, although both these methods work, they aren't solving the problem. I'm a bit poor when it comes to arrays, and sessions are really new to me. Here's the whole thing now... if ($_POST['makername'] != NULL) { if (!isset($_SESSION['sess_maker'])) { //if (!isset($_SESSION['testarray'])) { $temp_test = array(); $_SESSION['testarray']; } if (!isset($_SESSION['sess_maker'])) { $sess_maker = array(); $_SESSION['sess_maker']; } if (!isset($_SESSION['sess_location'])) { $sess_location = array(); $_SESSION['sess_location'];} if (!isset($_SESSION['sess_year'])) { $sess_year = array(); $_SESSION['sess_year']; } if (!isset($_SESSION['sess_image'])) { $sess_image = array(); $_SESSION['sess_image']; } if (!isset($_SESSION['sess_type'])) { $sess_type = array(); $_SESSION['sess_type']; } $sess_maker[] = $_POST['makername']; $sess_location[] = $_POST['makerlocation']; $sess_year[] = $_POST['makeryear']; $sess_image[] = $_POST['compareimage']; $sess_type[] = $_POST['instr_type']; } } if (isset($_SESSION['sess_maker'])){ //print_r($_SESSION['testarray']); //$temp_test = $_SESSION['testarray']; $sess_maker = $_SESSION['sess_maker']; $sess_location = $_SESSION['sess_location']; $sess_year = $_SESSION['sess_year']; $sess_image = $_SESSION['sess_image']; $sess_type = $_SESSION['sess_type']; if ($sess_maker == $_POST['makername']) { echo "Name already added."; } else { if (count($sess_maker) > 4 ) { $alert_maxreached = "<span style='color:#ff0000;'>You cannot add any more instruments to compare</span> "; // IMPORTANT - remove these before live use! //unset($sess_maker); //unset($sess_location); //unset($sess_year); //unset($sess_image); // Instead get an alert to state maxmimum comparisons reached } else { //$temp_test[] = $_POST['makername']; $sess_maker[] = $_POST['makername']; $sess_location[] = $_POST['makerlocation']; $sess_year[] = $_POST['makeryear']; $sess_image[] = $_POST['compareimage']; $sess_type[] = $_POST['instr_type']; //echo "Name added"; } //print_r($temp_test); } } if ($_POST['currency'] != NULL) { $_SESSION['currency'] = $_POST['currency']; } if (isset($_SESSION['currency'])){ } else { $_SESSION['currency'] = $_POST['currency']; } if ($_POST['remove']) { $removes = array('nothing_a','nothing_b','nothing_c','nothing_d','nothing_d'); if (isset($_POST['remove']) && in_array($_POST['remove'],$removes)) { $key = array_search($_POST['remove'],$removes); unset($sess_maker[$key]); unset($sess_location[$key]); unset($sess_year[$key]); unset($sess_image[$key]); unset($sess_type[$key]); } /*if ($_POST['remove'] == 'nothing_a') { unset($sess_maker[0]); unset($sess_location[0]); unset($sess_year[0]); unset($sess_image[0]); unset($sess_type[0]); } elseif ($_POST['remove'] == 'nothing_b') { unset($sess_maker[1]); unset($sess_location[1]); unset($sess_year[1]); unset($sess_image[1]); unset($sess_type[1]); } elseif ($_POST['remove'] == 'nothing_c') { unset($sess_maker[2]); unset($sess_location[2]); unset($sess_year[2]); unset($sess_image[2]); unset($sess_type[2]); } elseif ($_POST['remove'] == 'nothing_d') { unset($sess_maker[3]); unset($sess_location[3]); unset($sess_year[3]); unset($sess_image[3]); unset($sess_type[3]); } elseif ($_POST['remove'] == 'nothing_e') { unset($sess_maker[4]); unset($sess_location[4]); unset($sess_year[4]); unset($sess_image[4]); unset($sess_type[4]); }*/ $sess_maker = array_merge($sess_maker); $sess_location = array_merge($sess_location); $sess_year = array_merge($sess_year); $sess_image = array_merge($sess_image); $sess_type = array_merge($sess_type); array_pop($sess_maker); array_pop($sess_location); array_pop($sess_year); array_pop($sess_image); array_pop($sess_type); $_SESSION['sess_maker'] = $sess_maker; $_SESSION['sess_location'] = $sess_location; $_SESSION['sess_year'] = $sess_year; $_SESSION['sess_image'] = $sess_image; $_SESSION['sess_type'] = $sess_type; } if ($_POST['storecompare'] == 'on'){ $_SESSION['sess_maker'] = $sess_maker; $_SESSION['sess_location'] = $sess_location; $_SESSION['sess_year'] = $sess_year; $_SESSION['sess_image'] = $sess_image; $_SESSION['sess_type'] = $sess_type; //$_SESSION['testarray'] = $temp_test; } else { } Very stumped and very tired... Quote Link to comment https://forums.phpfreaks.com/topic/155038-whats-going-on-with-my-session-arrays/#findComment-815476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.