0

I'm trying to make a multiform wardrobe builder I have a session variable for the number of doors the user selected in the previous page (1-6 doors). Based on the number of doors selected I am displaying default doors(i.e no type of door range selected) using a for loop. My issue is that I want each door to have a unique variable and turn these into session variables (to use further in the application) such as door1, door2 and door3 if 3 doors are selected. The idea is for these doors to be unique so the user can click and highlight the door 1 default image and populate that area/image with an image from the list of door ranges. Fairly new to this so any help would be greatly appreciated

code:

<?php
$myDoors = array();
for ($x = 1; $x <= $selected_doors; $x++) {
    echo "Door: $x <br>";
    $myDoors["Door$x"] = "value set in loop";
      //if you want to loop through them all
    foreach ($myDoors as $key => $val) {
      echo "$key -> $val\n";
    }
    if ($selectRanges == 'Minimalist') { ?>
        <div>
            <img src="images/defaultMinimalist.png" alt="image">
        </div>
    <?php } elseif ($selectRanges == 'Classic') { ?>
        <div>
            <img src="images/defaultClassic.png" alt="image">
        </div>
    <?php } else { ?>
        <div>
            <img src="images/defaultEllipse.png" alt="image">
        </div>
    <?php }
} ?>

2 Answers 2

1

Don't use "Unique variables". $_SESSION is an array like any other. You can embed arrays in arrays, so...

$_SESSION['doors'][7]['style'] = 'Minimalist';
                  ^^^--- door ID

Now it's just a regular array, meaning you don't need to do silly stuff like

$id = 7;
$_SESSION["door{$id}"]['style'] = ...
Sign up to request clarification or add additional context in comments.

3 Comments

Is door id relative to $selected_doors? i.e. If you have 4 doors will the door id go from 1-4? to further be called. Trying to work it into my code.
that's up to you on how you assign those ids and build the array.
Thanks for your help!
0

First create the array of doors and then push the whole array to the session

$_SESSION['doors'] = $myDoors;

now you can access the doors in the session like this

$_SESSION['doors'][$doorNumber]

1 Comment

$_SESSION['doors'][$doorNumber] in the loop? How would I add this into my current code? Want to make sure it's right. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.