0

I've come across a problem and I don't know how I should approach it.

I want to have the user of my website able to pick from a selection of 5 things, say for example;

Cars, Trains, Planes, Snowmobiles, ATVs.

Based on their selection, it will redirect to a page with the variable ?choice=cars in the URL (this part I know how to do.) For example configure_choice.php?choice=cars

Then, upon visiting this page, I want to generate 8 dropdown boxes, each of which have a different HTML identifier. Note that these dropdown boxes VARY depending on the choice.

So, if they chose cars, I would want:

<select name="car1" class="dropdown">
<option>Chevrolet</option>
<option>GM</option>
<option>Cadillac</option>
</select>

<select name="car2" class="dropdown">
<option>Chevrolet</option>
<option>GM</option>
<option>Cadillac</option>
</select>

<select name="car3" class="dropdown">
<option>Chevrolet</option>
<option>GM</option>
<option>Cadillac</option>
</select>

... and so on, all the way to 8.

I thought about assigning something like

for($j = 1, $j <= 8, ++$j)
{
$car_dropdown_$j = "<select name=""car" . "$j"" class="dropdown">
    <option>Chevrolet</option>
    <option>GM</option>
    <option>Cadillac</option>
    </select>";

echo $car_dropdown_$j;
}

But when I try to run this loop, I get the following error:

Parse error: syntax error, unexpected ')', expecting ';' in C:\web\encodetest.php on line 5

And I also don't know if you can put a variable inside the HTML tag like that and have it work properly.

Basically I'm trying to set up 8 variables that I can just plop in my HTML somewhere with and have the dropdown boxes be unique, so when sent off to MySQL, they can be identified. Any help here?

Thanks so much!

3 Answers 3

1
for($j = 1; $j <= 8; $j++) //goodeye adam catching the ;
{
$car_dropdown[$j] = "<select name='car" . $j . "' class=\"dropdown\">
    <option>Chevrolet</option>
    <option>GM</option>
    <option>Cadillac</option>
    </select>";

echo $car_dropdown[$j];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Holy crap it worked! So you just created an array, and set each new $j value to a $car_dropdown AT that j value, correct?
yeah but it's not actually necessary. i'm just doing that because I assume you are trying to retain the $car_dropdown values at each individual $j for some reason. You could actually replace $car_dropdown[$j] with $anything and this code would still work. Your issue was that you were trying to declare two variables simultaneously ($car_dropdown and $j) in one statement, and also that you weren't escaping your <select> tags double-quote attributes
0

The correct syntax would be: for($j = 1; $j <= 8; ++$j). Statements are separated by ;.

To insert a variable into a string you can either do

echo "<select name=\"car" . $j . "\" class=\"dropdown\">"; 

or

echo "<select name=\"car$j\" class=\"dropdown\">"; 

Note that quotes inside a string should be escaped with \ or they would be treated as end of string.

1 Comment

Thanks, sorry. Even so, I get another error for an unexpected $j variable on line 7... implying it doesn't like the expression "$car_dropdown_$j"
0

There were a few syntax errors and the other answers didn't work either...

First you need to use ; in the for loop. For dynamic variables you need to use [$j].

To stick strings together you have to use " . $j . "

for($j = 1; $j <= 8; $j++){
    $car_dropdown[$j] = '<select name="car' . $j . '" class="dropdown">
        <option>Chevrolet</option>
        <option>GM</option>
        <option>Cadillac</option>
        </select>';

    echo $car_dropdown[$j];
}

1 Comment

goodeye! i'll edit it haha i just like that we took the exact same process (single quotes the name attribute, escape the class attribute haha.. one minute too late!)

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.