Jump to content

Recommended Posts

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

 

:'(

 

 

Link to comment
https://forums.phpfreaks.com/topic/155038-whats-going-on-with-my-session-arrays/
Share on other sites

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()

{

....

}

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

 

 

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

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.

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