0

I am having an issue where I create a loop of headings. I want to create a list of dates for each heading, these dates are pulled from MySQL as are the headings.

I think I am looking to loop within an loop.

I think once $h reaches $number_of_days it doesn’t reset even though it is within the 1st while loop. Is there a different way to go about this?

$h='1';
          while($report_types = $rt_type->fetch())
            {


$rt_types = $report_types['report_name'];
            echo '<div>'. $rt_types .'</div>';


while($h <= $number_of_days){          
            echo '<div class="report_day">'.$h.'</div>';
            $h++;

            }    
        }
5
  • while($h <= $number_of_days){ where did you initialize $h? Commented May 27, 2014 at 15:20
  • 4
    $h='1'; should be $h=1; to make it an integer. Commented May 27, 2014 at 15:21
  • 2
    @Biotox should be; but probably doesn't cause a problem. <= will do a loose comparison casting $h, and then $h++ will cast to an integer. Commented May 27, 2014 at 15:23
  • 1
    Other than the fact that $h='1' should be $h=1 what is the value of $number_of_days and where is it set? Commented May 27, 2014 at 15:26
  • 1
    What i see is that you don't need that while() loop. For example for($h=1;$h<=$number_of_days;$h++) { will end all $h issues. Commented May 27, 2014 at 15:28

3 Answers 3

3

I added a reset to $h before the next loop of $report_types

    $h=1;
    while($report_types = $rt_type->fetch())
    {
        $rt_types = $report_types['report_name'];
        echo '<div>'. $rt_types .'</div>';


        while($h <= $number_of_days){          
            echo '<div class="report_day">'.$h.'</div>';
            $h++;
        }

        $h=1; //RESET BEFORE NEXT LOOP
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Part of problem with your code is coming from formatting more than anything. The less readable code is, the harder it is to debug.

That said, here is my reworked version of your code:

while($report_types = $rt_type->fetch()) {
  $rt_types = $report_types['report_name'];
  echo '<div>' . $rt_types . '</div>';
  for ($h = 1; $h <= $number_of_days; $h++) {
    echo '<div class="report_day">' . $h . '</div>';
  }
}

What I did is remove the while loop & switched it to for loop which makes the logic a bit cleaner. So now your $h1 incrementing logic is all contained in this one line for ($h = 1; $h <= $number_of_days; $h++) { which makes it all easier to read & understand as well.

Also, as far as your original code goes, this assignment to $h is problematic:

$h='1';

When you place a value in quotes like that it makes the contents within those quotes a literal string. For numerical purposes like this you would set that as:

$h=1;

Comments

0

Put $h='1'; inside the first while.

So change:

$h='1';
while($report_types = $rt_type->fetch())
{

into:

while($report_types = $rt_type->fetch())
{
    $h='1';

Comments

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.