CodeIgniter Forums
if condition in foreach loop - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: if condition in foreach loop (/showthread.php?tid=16896)



if condition in foreach loop - El Forum - 03-19-2009

[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".


if condition in foreach loop - El Forum - 03-19-2009

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



if condition in foreach loop - El Forum - 03-19-2009

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


if condition in foreach loop - El Forum - 03-19-2009

[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>



if condition in foreach loop - El Forum - 03-19-2009

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