Welcome Guest, Not a member yet? Register   Sign In
problem with my set_select()
#1

[eluser]ajibolaabdullahi1[/eluser]
hello, i'm trying to populate a view file to edit user's profile.

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


the problem is that no default value is selected, what's the solution.
#2

[eluser]CroNiX[/eluser]
This isn't a ternary operator.
Code:
&lt;? ($role->id == $user->role) ? $default=TRUE : $default=FALSE; ?&gt;
#3

[eluser]ajibolaabdullahi1[/eluser]
@CroNiX
i don't understand, i actually tested the ternary operation above and it returns 1 or nothing
#4

[eluser]ojcarga[/eluser]
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
Code:
$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;

&lt;?=form_dropdown("roles", $arrayRoles, $selected);?&gt;

So, you will have something similar

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

Cheers!
#5

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

Code:
<select name="role">
        <option value="">select a role</option>
        &lt;? foreach ($roles as $role): ?&gt;    
                &lt;?  ($role->id == $user->role) ? $selected='selected' : $selected=''  ?&gt;
                
        <option value="&lt;?= $role-&gt;id ?&gt;" &lt;?= $selected ?&gt; > &lt;?= $role->name?&gt; </option>
        &lt;? endforeach; ?&gt;
    </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
Code:
$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

Code:
<label for="email">Email address:</label> &lt;input type="text" name="email" value="&lt;?=  set_value('email', $user-&gt;email)?&gt;" /&gt;
#6

[eluser]ojcarga[/eluser]
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!
#7

[eluser]Aken[/eluser]
[quote author="CroNiX" date="1348718746"]This isn't a ternary operator.
Code:
&lt;? ($role->id == $user->role) ? $default=TRUE : $default=FALSE; ?&gt;
[/quote]
As he said, this is ugly. You can assign the true/false result of a conditional to a variable instead - much cleaner.

Code:
$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.




Theme © iAndrew 2016 - Forum software by © MyBB