EllisLab text mark
Advanced Search
     
problem with my set_select()
Posted: 26 September 2012 11:03 PM   [ Ignore ]
Joined: 2012-09-26
3 posts

hello, i’m trying to populate a view file to edit user’s profile.

<select name="role">
        <
option value="">select a role</option>
        
<? foreach ($roles as $role): ?>     
                
                <? 
($role->id == $user->role) ? $default=TRUE $default=FALSE?>
        
<option value="<?= $role->id ?>" <?set_select('role'$role->id$default?> <?$role->name?> </option
        
<? endforeach; ?>
</select

the problem is that no default value is selected, what’s the solution.

 
Posted: 27 September 2012 12:05 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3819 posts

This isn’t a ternary operator.

<? ($role->id == $user->role) ? $default=TRUE $default=FALSE?> 

 

 Signature 
 
Posted: 27 September 2012 03:24 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-09-26
3 posts

@cronix
i don’t understand,  i actually tested the ternary operation above and it returns 1 or nothing

 
Posted: 27 September 2012 04:53 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

What you can do is if you are using the same number of rolID in the select (dropdown) index control just do something like this

$arrayRoles = array();                
foreach (
$roles as $key => $value{
    
//$arrayRoles[0] = "Admin"
    //$arrayRoles[1] = "AnyOther"
    
$arrayRoles[$key] $value["rol"];  
}

//so $selected would be the index of the array $arrayRoles[]
$selected 0;

<?=form_dropdown("roles"$arrayRoles$selected);?> 

So, you will have something similar

<option selected="selected" value="0">Admin</option>
<
option value="1">AnyOther</option

Cheers!

 
Posted: 27 September 2012 05:13 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-09-26
3 posts

@ojcarga
tnx 4 ur reply, if i understand what you mean, i could also have used something similar to

<select name="role">
        <
option value="">select a role</option>
        
<? foreach ($roles as $role): ?>     
                <?  
($role->id == $user->role) ? $selected='selected' $selected=''  ?>
                
        
<option value="<?= $role->id ?>" <?$selected ?> <?$role->name?> </option
        
<? endforeach; ?>
    
</select

which would select the users role on initial load of the page, but how do i then handle repopulating the new selected value in case of an error after

$this->fom_validation->run() 

Hence i’m trying to achieve two things, auto-select the users role in the DB on load of the page, and re-populate d selected value after the edit form is submitted in-case there’s an error.

The one for text input boxes work as desired, but i’m having problem with that of the select drop-down

<label for="email">Email address:</label> <input type="text" name="email" value="<?=  set_value('email', $user->email)?>" /> 
 
Posted: 27 September 2012 10:26 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-02
85 posts

I am not sure, I haven’t tested but why don’t you just use the form_dropdown() function of the form helper?
The third param of that function receive the value you wish to be selected.

Cheers!

 
Posted: 03 October 2012 01:24 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts
CroNiX - 27 September 2012 12:05 AM

This isn’t a ternary operator.

<? ($role->id == $user->role) ? $default=TRUE $default=FALSE?> 

As he said, this is ugly. You can assign the true/false result of a conditional to a variable instead - much cleaner.

$default = ($role->id == $user->role); 

You could even use the conditional directly in the set_select() function. Not as easily readable, but still valid.

Also, I agree to just using form_dropdown(). Then you can specify $user->role as the third parameter and it will be the default value.