1

I am struggling with the following code and cant find a away to fix it. The page just takes too long to load and "Fatal error: Maximum execution time of 30 seconds exceeded" error appears.

Maybe someone could explain or give an advice?

<?php

    $i = 1;
    $s = 1;
    $limit = 21;
    $seasons = 6;

    while ($s < $seasons) {
        if ($s < 10) {
            $s = '0' . $s;
        }

        while ($i < $limit) {

            if ($i < 10) {
                $i = '0' . $i;
            }
            echo '<input type="checkbox" value="S' . $s . 'E' . $i . '" name="rate"/>S' . $s . 'E' . $i . '<br/>';
            ++$i;
        }
    }
    ?>

Thank you

UPDATE:

After adding an $s++; at the end of firt loop the code was solved of errors and long load but it runs only once no respecting the $seasons = 6 condition. Any idea why would that happen? I tried to remove the 0 prefix and it still not looping till 6

UPDATE: Problem solved, second loop placed in function and it worked. Thanks to all!.

FINAL VERSION

<?php

    $s = 1;
    $limit = 21;
    $seasons = 7;

    while ($s < $seasons) {

        if ($s < 10) {
            $s = '0' . $s;
        }

        episodes($s,$limit);

    $s++;   
    }

    function episodes($s,$limit){
    $i = 1;

    while ($i < $limit) {
            if ($i < 10) {
                $i = '0' . $i;
            }           
           echo '<input type="checkbox" value="S' . $s . 'E' . $i . '" name="rate"/>S' . $s . 'E' . $i . '<br/>';    
            ++$i;
        }
    }

?>
2
  • 1
    You aren't incrementing $s anywhere, so it's always going to be less than $seasons (endless loop). You are adding a zero to the start of it, but that doesn't affect the loop... is that zero supposed to be at the end? Commented Feb 21, 2014 at 2:52
  • @scrowler thank you. Incrementing the S solved long loading page issue and no errors show up but the first loop only runs once, why it wont run 5 times as $seasons = 6 Commented Feb 21, 2014 at 9:22

3 Answers 3

2

First, be sure that you are comparing integers to integers when you execute your comparisons. It is okay to add '0' before your number if you need it for display purposes, but do that in the part that generates the display value instead of in the $s variable itself:

$s_formatted = str_pad($s, 2, '0', STR_PAD_LEFT);
echo '<input type="checkbox" value="S' . $s_formatted . 'E' . $i . '" name="rate"/>S' . $s . 'E' . $i . '<br/>';
 ++$i;

This allows you to remove the '0' prefix in the part of the code where you currently do it. You can remove these lines:

if ($s < 10) {
    $s = '0' . $s;
}

Finally, be sure to increment $s. In your current code, $s < $seasons is always true because the value of $s never changes. If you want $s to increase with each loop, increment it like this:

$s++;
Sign up to request clarification or add additional context in comments.

1 Comment

weird but $s_formatted... did not work in my case. S shows up without 0. Also the first loop seems to run only once up to S1E20 but why it wont continue S02E01?
1
$s = '0' . $s;

This just add a prefix 0 before $s, $s will be '01', '001', '0001' ... and so on.

Then compare with the integer 6, $s will converted to 1,

So $s will never over $seasons, it will cause infinite loop.

The same with $i, the infinite loop is actually in the nested while loop.(++$i;)

If you really want the string prefixed with 0, then you should check the string length instead.

Example:

if (strlen($s) < 10) {

Comments

-1

you are missing $s++ end of you outer while,run this code:

$i = 1;
$s = 1;
$limit = 21;
$seasons = 6;

while ($s < $seasons) {
    if ($s < 10) {
        $s = '0' . $s;
    }

    while ($i < $limit) {

        if ($i < 10) {
            $i = '0' . $i;
        }
        echo '<input type="checkbox" value="S' . $s . 'E' . $i . '" name="rate"/>S' . $s . 'E' . $i . '<br/>';
        ++$i;
    }
$s++;
}

4 Comments

How do you know he wants to do increase $s by one each time? You're purely assuming - it could be ten, tied into the if statement at the start... it could be anything.
@scrowler,if he want increase more than one by one time ,he could just change the code $++ to $s = $s+$whateverTheNumberHeWantToIncrease,this is not the main problem here,main problem is he missed increse $s...am i right?
You're right in that he has missed an increment/decrement of that variable (he's got ++$i for example, but not $s) - but you're taking a stab in the dark with the value to increase it by.
@scrowler,fine two reputations...O__O"…

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.