auto select name from select option

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

auto select name from select option

Post by insight »

I'm creating a menu where a user can add an access level to it via an select option. I have managed to get it to select and add it to database when a user creates a link. But I don't know how to get it to automatically select the access level option when they go to edit the link. Can this be done by an if-else statement, and if yes, can somebody please show me how?

So far I can get checkboxes to automatically be ticked by this code:

Code: Select all

if ($underline == '1'){
echo "<input type=\"checkbox\" name=\"underline\" value=\"1\" checked>";
}
else{
echo "<input type=\"checkbox\" name=\"underline\" value=\"1\">";
}
But I don't know how to do this for a select option.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: auto select name from select option

Post by jackpf »

You can use "selected" instead of checked.

The principal is the same. I do it slightly different though. Take this example:

Code: Select all

$name = 'jimmy'; //an example;
 
$selected = array($name => 'selected="selected"');
 
$names = array('ben', 'jimmy', 'john');
 
echo '<select>';
foreach($names as $key => $value)
    echo '<option '.((isset($selected[$value])) ? $selected[$value] : null).'>'.$value.'</option>';
echo '</select>';
If you change $name to a different name, you should see that one become selected.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: auto select name from select option

Post by insight »

jackpf wrote:You can use "selected" instead of checked.

The principal is the same. I do it slightly different though. Take this example:

Code: Select all

$name = 'jimmy'; //an example;
 
$selected = array($name => 'selected="selected"');
 
$names = array('ben', 'jimmy', 'john');
 
echo '<select>';
foreach($names as $key => $value)
    echo '<option '.((isset($selected[$value])) ? $selected[$value] : null).'>'.$value.'</option>';
echo '</select>';
If you change $name to a different name, you should see that one become selected.
How will this work if I have more then one name in the select option. For example, how will I implement your code in this:

Code: Select all

<td class=\"tbl\" rowspan=\"4\" style=\"padding-right: 10px;\"><select name=\"level\" size=\"7\"><option value=\"Public\">-&nbsp;Public</option><option value=\"Registered\">&nbsp;&nbsp;&nbsp;-&nbsp;Registered</option><option value=\"VIP\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;VIP</option><option value=\"Team\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;Team</option><option value=\"Moderator\">-&nbsp;Moderator</option><option value=\"admin\">&nbsp;&nbsp;&nbsp;-&nbsp;Administrator</option><option value=\"superadmin\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;Super Administrator</option></select></td>
And for example, if a user creates a new link and selects the access level to be for example VIP. The next time that (or another) user goes to edit that link the name VIP will be automatically selected. Will your way work for this?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: auto select name from select option

Post by jackpf »

Test it out.

Do you understand the code I posted? If not, I'm happy to explain it.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: auto select name from select option

Post by insight »

jackpf wrote:Test it out.

Do you understand the code I posted? If not, I'm happy to explain it.
Kind of. If I understand correctly (sorry, I'm new to PHP) then:

Code: Select all

$name = 'jimmy'; //an example; //An example name;
 
$selected = array($name => 'selected="selected"'); //If name is selected then add this?;
 
$names = array('ben', 'jimmy', 'john'); //names in array that are allowed;
 
echo '<select>';
foreach($names as $key => $value)// Have no idea
    echo '<option '.((isset($selected[$value])) ? $selected[$value] : null).'>'.$value.'</option>'; // The $selected[$value'] adds the 'selected="selected" from the $selected variable ONLY if the $name matches the one in the array. If not then it is set to null?
echo '</select>';
Well I think I get some of it, but maybe you can explain a little better :P
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: auto select name from select option

Post by jackpf »

Exactly :D

The same principal applies to the userlevels, or anything that you may be setting.

You just need to put the actual value of whatever it is in place of $name (and you may want to rename the variables, obviously).

I happened to use a foreach loop, but the same can be done with mysql_fetch_array() in a while loop.
insight
Forum Commoner
Posts: 52
Joined: Tue Jul 07, 2009 9:12 am

Re: auto select name from select option

Post by insight »

It worked beautifully, thanks a million :D

One problem though How can I add &nbsp; before the names? I've the following without any luck. I don't really require it, it just makes it look more neat :P

Code: Select all

                $selected = array($level => 'selected="selected"');
                
                $names = array('-&nbsp;Public', '&nbsp;&nbsp;&nbsp;-&nbsp;Registered', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;VIP', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;Team', '-&nbsp;Moderator', '&nbsp;&nbsp;&nbsp;-&nbsp;Administrator', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;Super Administrator');
                
                echo '<select name=\"level\" size=\"7\">';
                    foreach($names as $key => $value)
                    echo '<option '.((isset($selected[$value])) ? $selected[$value] : null).'>'.$value.'</option>';
                echo '</select>';
EDIT: Just thought I'd add, I know why it doesn't work, I'm just curious to know how I can make it work.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: auto select name from select option

Post by jackpf »

Put the non breaking spaces into the echo, not into the array.

I guess you could have a counter or something that increments the number of spaces displayed, since it looks like that's what you're doing.

And no, it won't work, since $level probably doesn't have all them spaces in.
Post Reply