Jump to content

Need help with my plugging array into scroll list code.


pxxb
Go to solution Solved by pxxb,

Recommended Posts

Hi, I am trying to make a scroll list that is multi-selectable. I am having trouble generating the scroll list and having the array be in it. I am new to php so any help would be nice. Thank you.

 

$fruits = array(        "--" => "---Please Select a Fruit ---",
                        "ap"=>"Apple",
                        "ba"=>"Banana",
                        "ga"=>"Grapes",
                        "wa"=>"Watermelon",
                        "pe"=>"Pear");

 

<fieldset>
<legend> Fruits</legend>
<select name = "fruits">
<?php foreach ($fruits as $fruit){ ?>
<option value = ""><?php $fruit ?></option>
<?php } ?>
</select>
</fieldset>

Link to comment
Share on other sites

I assume you have the $fruit = array(...) code wrapped in <?php and ?> tags? If you have then you you've almost got it. The <?php $fruit ?> should be <?php echo $fruit ?>.
 

If you want to set the value="" for each option, eg ap, ba, ga etc. Then the foreach loop construct needs to be foreach ($fruits as $key => $fruit) to retrieve the key value pairs from the $fruits associative array. Now you can do <option value="<?php echo $key; ?>"> to set each options value

 

Corrected code

<?php

$fruits = array(
    "--" => "---Please Select a Fruit ---",
    "ap" =>"Apple",
    "ba" =>"Banana",
    "ga" =>"Grapes",
    "wa" =>"Watermelon",
    "pe" =>"Pear"
);
?> 
<fieldset>
<legend> Fruits</legend>
<select name = "fruits">
<?php foreach ($fruits as $key => $fruit){ ?>
<option value = "<?php echo $key ?>"><?php echo $fruit ?></option>
<?php } ?>
</select>
</fieldset>
Link to comment
Share on other sites

  • Solution

Thank you, it worked when I executed it seperately. This code is part of a bigger program that I am working on and for somereason it wont display the $fruits. :\ trying to figure out why haha

 

I assume you have the $fruit = array(...) code wrapped in <?php and ?> tags? If you have then you you've almost got it. The <?php $fruit ?> should be <?php echo $fruit ?>.
 

If you want to set the value="" for each option, eg ap, ba, ga etc. Then the foreach loop construct needs to be foreach ($fruits as $key => $fruit) to retrieve the key value pairs from the $fruits associative array. Now you can do <option value="<?php echo $key; ?>"> to set each options value

 

Corrected code

<?php

$fruits = array(
    "--" => "---Please Select a Fruit ---",
    "ap" =>"Apple",
    "ba" =>"Banana",
    "ga" =>"Grapes",
    "wa" =>"Watermelon",
    "pe" =>"Pear"
);
?> 
<fieldset>
<legend> Fruits</legend>
<select name = "fruits">
<?php foreach ($fruits as $key => $fruit){ ?>
<option value = "<?php echo $key ?>"><?php echo $fruit ?></option>
<?php } ?>
</select>
</fieldset>
Link to comment
Share on other sites

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.