0

I am using the following which basically prints a dropdown box showing all hours with 30 minute increments:

$start = '12:00AM';
$end = '11:59PM';
$interval = '+30 minutes';

$start_str = strtotime($start);
$end_str = strtotime($end);
$now_str = $start_str;

echo '<select name="doors_open">';

while($now_str <= $end_str){
    echo '<option value="' . date('h:i A', $now_str) . '">' . date('h:i A', $now_str) . '</option>';
    $now_str = strtotime($interval, $now_str);
}

echo '</select>';

BUT, i want to do it twice for another field further down the page... question is, how do I reset the loop?

Here's what I've tried but doesn't work:

echo '<select name="event_start">';

reset($now_str);
reset($end_str);
while($now_str <= $end_str){
    echo '<option value="' . date('h:i A', $now_str) . '">' . date('h:i A', $now_str) . '</option>';
    $now_str = strtotime($interval, $now_str);
}

echo '</select>';

2 Answers 2

3

In your case you'd just have to reassign the initial value to $now_str so your loop can run again. As your $start_str variable contains it, you could just reassign it to $now_str after the first loop.

$now_str = $start_str;

The reset function actually resets an array internal pointer used for loops, which is not the case here.

Note: I really recommend to use a helper function as @ComFreek recommended to avoid repeating code.

Sign up to request clarification or add additional context in comments.

2 Comments

@ComFreek I'm really sorry for mistyping your name like this, hahaha. It was not proposital.
No problem, that happens quite often ;)
2

reset() resets the internal cursor of an array. That's a totally different thing than you want to achieve.

You can of course simply duplicate the code, but this neither good code design nor DRY.

The best you can do is to put the whole HTML generation into a helper function:

function genSelect($selectName, $start, $end, $interval) {
  $str = "<select name='$selectName'>";

  $start_str = strtotime($start);
  $end_str = strtotime($end);
  $now_str = $start_str;


  while($now_str <= $end_str){
    $str .= '<option value="' . date('h:i A', $now_str) . '">' . date('h:i A', $now_str) . '</option>';
    $now_str = strtotime($interval, $now_str);
  }

  $str .= '</select>';
  return $str;
}

Then, you can call it as often as you want:

echo genSelect('doors_open', '12:00AM', '11:59PM', '+30 minutes');

You may also consider caching the result:

$doorsOpenHtml = genSelect('doors_open', '12:00AM', '11:59PM', '+30 minutes');

4 Comments

Would have to add another variable to the function otherwise all would have same name
@DarrenSweeney I added one.
Many thanks! Is there a reason to return $str and then echo the function, as opposed to echo $str within the function then just call the function without echo? Just curious
@DarrenSweeney It might be cleaner to return the string instead of directly echoing it. For instance, what would you do if you need to further process the output of genSelect() (using direct echos)? You would need to use output buffering functions (ob_*) which are quite messy in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.