Welcome Guest, Not a member yet? Register   Sign In
if condition in foreach loop
#1

[eluser]runrun[/eluser]
Hi,

I am trying set default value for a select menu in the site's user profile section.

In detail, I need to load the date of birth of user to a select menu, this happens when user view their profile, and this profile is editable.

controller:
Code:
//get the date of birth of user from database
$username = $this->session->userdata('username');
$this->db->select(date)->from('user')->where('username',$username);
$query = $this->db->get();    
$data['row'] = $query->row();
$data['Dchoices'] = array_merge(array('' => 'date'), range(1,31));

view:
Code:
<?php foreach($Rchoices as $Rkey=>$Rchoice):?>
<option value ="&lt;?=$Rkey?&gt;" &lt;?=set_select('region', $Rkey, if($Rkey=$row->date){return TRUE;}else{return FALSE;})?&gt;>&lt;?=$Rchoice?&gt;</option>
&lt;?php endforeach; echo"\n";?&gt;

I put the if condition in the third parameter of set_select. I get error "unexpected T_IF".
#2

[eluser]pistolPete[/eluser]
Try this:
Code:
<option value ="&lt;?=$Rkey?&gt;" &lt;?=set_select('region', $Rkey, ($Rkey == $row->date) )?&gt;>&lt;?=$Rchoice?&gt;</option>
#3

[eluser]runrun[/eluser]
I think I third parameter has to be "TRUE", in order to make that option default selected
#4

[eluser]Armchair Samurai[/eluser]
Code:
<option value ="&lt;?=$Rkey;?&gt;" &lt;?=set_select('region', $Rkey, $Rkey == $row->date ? TRUE : FALSE);?&gt;>&lt;?=$Rchoice;?&gt;</option>
#5

[eluser]runrun[/eluser]
@pistolPete: sorry, that really works,I mistakenly put the $Rkey==$row->date in the "month" option, LOL.
@Armchair Samurai: thanks man.




Theme © iAndrew 2016 - Forum software by © MyBB