Jump to content

[SOLVED] For loop to output 01,02,03...59


Dark-Hawk

Recommended Posts

Alright I'm creating a drop down menu that will be used to select a range of time. I ultimately want it to display 00, through 59 and am having issues creating a proper for loop to display 01, 02, etc. I know I'll need to use nested loops but I'm unsure exactly what to do. I was able to come up with/find:

<?
for($a=0; $a < 10; $a++){
    for($b=0; $b < 10; $b++){
        echo $a.$b.", ";
    }
}
?>

Which counts out to 99 .. I'm having issues getting it to break at 59.

Link to comment
https://forums.phpfreaks.com/topic/158586-solved-for-loop-to-output-01020359/
Share on other sites

Ah ok, that would've worked too, well thanks for the help, here's the code as it sits now working as I need it:

<td><select name="hour1">
<? for($x = 1; $x <= 12; $x++) { ?>
  <option value="<? echo $x; ?>"><? echo $x; ?></option><? } ?>
  </select><b>:</b>
  <select name="minute1">
<? for($a=0; $a < 6; $a++){
      for($b=0; $b < 10; $b++){ ?>
  <option value="<? echo "$a$b"; ?>"><? echo "$a$b"; ?></option>
  <? } }?>
  </select>
  <b>-</b>
  <select name="hour2">
<? for($x = 1; $x <= 12; $x++) { ?>
  <option value="<? echo $x; ?>"><? echo $x; ?></option><? } ?>
  </select><b>:</b>
  <select name="minute2">
<? for($a=0; $a < 6; $a++){
      for($b=0; $b < 10; $b++){ { ?>
  <option value="<? echo "$a$b"; ?>"><? echo "$a$b"; ?></option><? } ?>
  <? } }?>
  </select>
</td>

Woaw, I'm impressed you can still read that..

for($b=0; $b < 10; $b++){ ?>
  <option value="<? echo "$a$b"; ?>"><? echo "$a$b"; ?></option>
  <? } }?>

 

Is a lot more readable to me if it was typed down like say..

for($b=0; $b < 10; $b++){ 
    echo "<option value='$a$b'>$a$b</option>";
} }?>

 

And it's not adviced to use <? unless you're sure your script will ALWAYS run on a server with shorttags enabled. (use <?php instead)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.