Page 1 of 1
auto select name from select option
Posted: Thu Jul 30, 2009 9:27 am
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.
Re: auto select name from select option
Posted: Thu Jul 30, 2009 9:44 am
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.
Re: auto select name from select option
Posted: Thu Jul 30, 2009 11:00 am
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\">- Public</option><option value=\"Registered\"> - Registered</option><option value=\"VIP\"> - VIP</option><option value=\"Team\"> - Team</option><option value=\"Moderator\">- Moderator</option><option value=\"admin\"> - Administrator</option><option value=\"superadmin\"> - 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?
Re: auto select name from select option
Posted: Thu Jul 30, 2009 11:04 am
by jackpf
Test it out.
Do you understand the code I posted? If not, I'm happy to explain it.
Re: auto select name from select option
Posted: Thu Jul 30, 2009 9:14 pm
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

Re: auto select name from select option
Posted: Thu Jul 30, 2009 9:28 pm
by jackpf
Exactly
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.
Re: auto select name from select option
Posted: Fri Jul 31, 2009 2:37 am
by insight
It worked beautifully, thanks a million
One problem though How can I add before the names? I've the following without any luck. I don't really require it, it just makes it look more neat
Code: Select all
$selected = array($level => 'selected="selected"');
$names = array('- Public', ' - Registered', ' - VIP', ' - Team', '- Moderator', ' - Administrator', ' - 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.
Re: auto select name from select option
Posted: Fri Jul 31, 2009 6:13 am
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.