CodeIgniter Forums
Weird comparison results using set_select - probably PHP's fault - 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: Weird comparison results using set_select - probably PHP's fault (/showthread.php?tid=26756)



Weird comparison results using set_select - probably PHP's fault - El Forum - 01-22-2010

[eluser]mattpointblank[/eluser]
Hi all.

My application has three <select> boxes for choosing time - hours / minutes / AM or PM.

I'm reading each of these values back out of the database and want to set the boxes to their appropriate values if they were already set.

My code looks like this:

Code:
<select name="startTimeHours">
    <option value="01" &lt;?php echo set_select('startTimeHours', '01', @$StartTimeHours == 01); ?&gt;>01</option>
    <option value="02" &lt;?php echo set_select('startTimeHours', '02', @$StartTimeHours == 02); ?&gt;>02</option>
// etc
</select>

This code is meant to work when adding a new record as well as editing an existing one (hence the somewhat-hacky @ sign preceding the variable). When editing it works fine, it selects the correct row. When adding a new row, however, the form comes with '09' in the Hours dropdown pre-selected. Since $StartTimeHours doesn't exist when adding a new row (trying to echo it produced an error), I don't know why the code has decided that it's equal to '09'.

Does anyone have any idea?


Weird comparison results using set_select - probably PHP's fault - El Forum - 01-22-2010

[eluser]mattpointblank[/eluser]
Never mind, got it. Changed the third parameter of set_select from:
Code:
set_select('startTimeHours', '01', @$StartTimeHours == 01)
to
Code:
set_select('startTimeHours', '01', @$StartTimeHours == '01')