How do I access an array outside if statement? Send data with AJAX

Hello, I’ve fixed the problem of getting the ajax data and sorting it by key, but now another problem has arisen.
When my array is declared outside if (isset ($ _ POST [’’ pColors]), it remains empty.
if aray is outside if output:
Array()

if aray is inside if output:

Array
(
    [5] => Array
        (
            [pColors] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )
        )
)

This is the code I use:

													$x = $getProductID;
													$_SESSION['pColors'] = array(); // outside

													if(isset($_POST['pColors'])) {
														$_SESSION['pColors'] = array(); // inside
														foreach($_POST as $colorKey => $value) {
															if ($colorKey == 'pColors') { 
																$_SESSION['pColors'][$x][$colorKey] = $value;
															}
														}
													}
												
													if (!empty($_SESSION['pColors'])) { 
														echo 'empty ! ';
													} else { echo 'none empty ! '; }

													print_r($x);
													echo '<pre>';
													print_r($_SESSION['pColors']);
													echo '</pre>';

You are not filling up the outer array with anything.

Hello ! What do you mean ?
$_SESSION[‘pColors’][$x][$colorKey] = $value;

but you are declaring a blank array array() outside of the loop. Unlike other languages, you do not have to declare this variable before using it. You can simply declare the session variable in the loop and use it immediately. If, for some reason, you wish to declare it outside of the loop:

if(isset($_POST['pColors'])) {
  $_SESSION['pColors'] = array(); // inside
  foreach($_POST as $colorKey => $value) {
    if ($colorKey == 'pColors') { 
    $_SESSION['pColors'][$x][$colorKey] = $value;
  }
  if (!empty($_SESSION['pColors'])) {
    echo 'not empty = !empty ';
  } else {
    echo 'empty ';
  }
}
}

also, notice that !empty means not (!) empty.

while we are at it, here is a better design for you because i don’t see a need to declare your session variable outside of the loop. Furthermore, the if block is useless and slows down the flow:

if(!empty(isset($_POST['pColors']))) {
  $_SESSION['pColors'] = array(); // inside
  foreach($_POST as $colorKey => $value) {
    if ($colorKey == 'pColors') { 
    $_SESSION['pColors'][$x][$colorKey] = $value;
  }
}

i am very tired today, i did not sleep much. I had a physical therapy appointment. Anyway, i don’t think that you even need to declare the session variable as an array. I think that you can just build it as an array. correct me if i’m wrong:

if(!empty(isset($_POST['pColors']))) {
  foreach($_POST as $colorKey => $value) {
    $_SESSION['pColors'][$x][$colorKey] = $value;
  }
}

Thanks ! We all are so little or very :slight_smile: Yes, it can also be declared inside, but the problem is that when it’s inside it is replaced by getting new data to $ _POST and the goal is to record it in the array and then get it out.

I tried to rotate it in many ways, but every time I get data to $ _POST, the array is overwritten. The last way I tried was:

if(!empty(isset($_POST['pColors']))) {
    $x = $getProductID;
    /$_SESSION['pColors'] = array();
    foreach($_POST as $colorKey => $value) {
    	if ($colorKey == 'pColors') {
    		$_SESSION['pColors'][$x][$colorKey] = $value;
    	}
    }
}

It’s also rewritten, but it’s over and over

you are not understanding me: although you can declare the array, it is not necessary.
you test if not empty but echo empty.
even if you wish to fill an array outside of the loop, you do not have to declare the session variable as an array. it is an array when you build it as an array.

back up your file then rewrite it as shown. the array will be filled with the posted values.

Thanks! I rewrote the code as you cut it above. The data I receive are:
When I send: getpID = 3; pColors: 1,2,3,4:

Array
(
    [3] => Array
        (
            [pColors] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )

            [getpID] => 3
        )
)

When I send the following data: getpID = 5; pColors: 1,2:

Array
(
[3] => Array
    (
        [pColors] => Array
            (
                [0] => 1
                [1] => 2
            )

        [getpID] => 5
    )

[5] => Array
    (
        [pColors] => Array
            (
                [0] => 1
                [1] => 2
            )

        [getpID] => 5
    )

)

so it is working for you. now you are expecting the arrays to be separated? then you must assign them to a session variable with a different name. so pid 3 should not be pcolors, it should be pcolors3:

$xfactor = 'pColors' . $x;
$_SESSION[$xfactor]

et cetera

I recommend that you read the reply you got at the end of your last thread for this problem. It defines a data structure for your cart that relates the product id with the color id and the quantity of each color and has example code that correctly implements that data structure.

1 Like

i thought that this code looked familiar. i remember you now, since phdr has posted.

that code from phdr is beautiful and it even implements a fast and simple switch. you should be using this code by phdr instead of what you are doing in this thread. phdr writes wonderful code. I recommend that you go back to that post.

Yes, definitely this will happen. Thanks!
How can I divide the times between the two fields?

// from this take colorID    <input type="checkbox" name="qty['.$getColorTitle['uniqueID'].']" class="css-checkbox pColor" value="1" id="cb-'.$getColorTitle['uniqueID'].'" />
    <label for="cb-'.$getColorTitle['uniqueID'].'"><img src="'.$colorFolder.''.$rowColors['image'].'" height="50px" />
    <div class="colorsC">
   // from this take quantity <input type="hidden" name="" value="0" style="width: 45px;" class="qtyC" />
    </div>
    </label>
Sponsor our Newsletter | Privacy Policy | Terms of Service