nkosinathi Posted April 14, 2010 Share Posted April 14, 2010 I have the code below which manually sets the default (select element default <option>) on the select element to 07:13. I want to use the current time as a default<option>. I know how to get the current time using PHP, my problem is how to write some for loop or something similar to do this ? Below is the manual code that sets the current time to 07:13 $tblString.="<td><select name=\"hour\" id=\"hour\" class=\"TextBoxes\">"; $tblString.="<option value=\"0\">0</option>"; $tblString .= "<option value=\"1\">1</option>"; $tblString .= " <option value=\"2\">2</option>"; $tblString .= " <option value=\"3\">3</option>"; $tblString .= " <option value=\"4\">4</option>"; $tblString .= " <option value=\"5\">5</option>"; $tblString .= " <option value=\"6\">6</option>"; $tblString .= "<option selected=\"selected\" value=\"7\">7</option>"; $tblString .= " <option value=\"8\">8</option>"; $tblString .= " <option value=\"9\">9</option>"; $tblString .= " <option value=\"10\">10</option>"; $tblString .= " <option value=\"11\">11</option>"; $tblString .= " <option value=\"12\">12</option>"; $tblString .= " <option value=\"13\">13</option>"; $tblString .= " <option value=\"14\">14</option>"; $tblString .= " <option value=\"15\">15</option>"; $tblString .= " <option value=\"16\">16</option>"; $tblString .= " <option value=\"17\">17</option>"; $tblString .= " <option value=\"18\">18</option>"; $tblString .= " <option value=\"19\">19</option>"; $tblString .= " <option value=\"20\">20</option>"; $tblString .= " <option value=\"21\">21</option>"; $tblString .= " <option value=\"22\">22</option>"; $tblString .= " <option value=\"23\">23</option>"; $tblString .= " </select> :"; $tblString .= "<select name=\"minute\" id=\"minute\" class=\"TextBoxes\">"; $tblString .= " <option value=\"00\">00</option>"; $tblString .= " <option value=\"01\">01</option>"; $tblString .= " <option value=\"02\">02</option>"; $tblString .= " <option value=\"03\">03</option>"; $tblString .= " <option value=\"04\">04</option>"; $tblString .= " <option value=\"05\">05</option>"; $tblString .= " <option value=\"06\">06</option>"; $tblString .= " <option value=\"07\">07</option>"; $tblString .= " <option value=\"08\">08</option>"; $tblString .= " <option value=\"09\">09</option>"; $tblString .= " <option value=\"10\">10</option>"; $tblString .= " <option value=\"11\">11</option>"; $tblString .= " <option value=\"12\">12</option>"; $tblString .= " <option selected=\"selected\" value=\"13\">13</option>"; $tblString .= " <option value=\"14\">14</option>"; $tblString .= " <option value=\"15\">15</option>"; $tblString .= " <option value=\"16\">16</option>"; $tblString .= " <option value=\"17\">17</option>"; $tblString .= " <option value=\"18\">18</option>"; $tblString .= " <option value=\"19\">19</option>"; $tblString .= " <option value=\"20\">20</option>"; $tblString .= " <option value=\"21\">21</option>"; $tblString .= " <option value=\"22\">22</option>"; $tblString .= " <option value=\"23\">23</option>"; $tblString .= " <option value=\"24\">24</option>"; $tblString .= " <option value=\"25\">25</option>"; $tblString .= " <option value=\"26\">26</option>"; $tblString .= " <option value=\"27\">27</option>"; $tblString .= " <option value=\"28\">28</option>"; $tblString .= " <option value=\"29\">29</option>"; $tblString .= " <option value=\"30\">30</option>"; $tblString .= " <option value=\"31\">31</option>"; $tblString .= " <option value=\"32\">32</option>"; $tblString .= " <option value=\"33\">33</option>"; $tblString .= " <option value=\"34\">34</option>"; $tblString .= " <option value=\"35\">35</option>"; $tblString .= " <option value=\"36\">36</option>"; $tblString .= " <option value=\"37\">37</option>"; $tblString .= " <option value=\"38\">38</option>"; $tblString .= " <option value=\"39\">39</option>"; $tblString .= " <option value=\"40\">40</option>"; $tblString .= " <option value=\"41\">41</option>"; $tblString .= " <option value=\"42\">42</option>"; $tblString .= " <option value=\"43\">43</option>"; $tblString .= " <option value=\"44\">44</option>"; $tblString .= " <option value=\"45\">45</option>"; $tblString .= " <option value=\"46\">46</option>"; $tblString .= " <option value=\"47\">47</option>"; $tblString .= " <option value=\"48\">48</option>"; $tblString .= " <option value=\"49\">49</option>"; $tblString .= " <option value=\"50\">50</option>"; $tblString .= " <option value=\"51\">51</option>"; $tblString .= " <option value=\"52\">52</option>"; $tblString .= " <option value=\"53\">53</option>"; $tblString .= " <option value=\"54\">54</option>"; $tblString .= " <option value=\"55\">55</option>"; $tblString .= " <option value=\"56\">56</option>"; $tblString .= " <option value=\"57\">57</option>"; $tblString .= " <option value=\"58\">58</option>"; $tblString .= " <option value=\"59\">59</option>"; $tblString .="</select>"; $tblString .= "</td>"; $tblString .="</tr>"; Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2010 Share Posted April 14, 2010 You're using a programming language, don't make things difficult on yourself. Use the PHP to create the select lists: <?php $hourSelect = "<select name=\"hour\">\n"; $currentHour = date('H'); for($hour=0; $hour<=23; $hour++) { $hourStr = str_pad($hour, 2, '0', STR_PAD_LEFT); $selected = ($hour==$currentHour) ? ' selected="selected"': ''; $hourSelect .= "<option value=\"{$hourStr}\"{$selected}>{$hourStr}</option>\n"; } $hourSelect .= "</select>\n"; $minuteSelect = "<select name=\"minute\">\n"; $currentMinute = date('i'); for($minute=1; $minute<=60; $minute++) { $minStr = str_pad($minute, 2, '0', STR_PAD_LEFT); $selected = ($minute==$currentMinute) ? ' selected="selected"': ''; $minuteSelect .= "<option value=\"{$minStr}\"{$selected}>{$minStr}</option>\n"; } $minuteSelect .= "</select>\n"; ?> Time: <?php echo $hourSelect; ?>:<?php echo $minuteSelect; ?> Quote Link to comment Share on other sites More sharing options...
nkosinathi Posted April 15, 2010 Author Share Posted April 15, 2010 Thanks a lot mjdamato: Would you kindly explain this line of code $selected = ($minute==$currentMinute) ? ' selected="selected"': ''; Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 15, 2010 Share Posted April 15, 2010 That's called a ternary operator. It's sort of a shorthand version of an If/Else statement. In this case I am using it to assign a value to $selected. If the condition in the parens is true, then the first value is assigned: ' selected="selected"'. If the condition is false then the second value is assigned: ''. I then use that value to set the appropriate option to selected in the HTML. Here is a illustrative example: $var = ([CONDITION]) ? [TRUE VALUE] : [FALSE VALUE] ; $a = 1; $result = ($a == 1) ? 'The variable $a equals 1' : 'The variable $a does not equal 1'; echo $result; //Output: The variable $a equals 1 $a = 2; $result = ($a == 1) ? 'The variable $a equals 1' : 'The variable $a does not equal 1'; echo $result; //Output: The variable $a does not equal 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.