Jump to content

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.