zritter
zritter

Reputation: 3

How can I use an if/else statement to iterate through an sql result array?

I need to produce a monthly visitation report, further subdivided into weekly summaries for each month. I have an SQL result array that contains the month number, week number, and week start and end dates.

Month   Week    week start  weekend
1       1       12/27/2015  1/2/2016
1       2       1/3/2016    1/9/2016
...
2       6       1/31/2016   2/6/2016
2       7       2/7/2016    2/13/2016

I want to loop through this array and create tables for each week, if they all are part of the same month. Then at the end of the month, echo a monthly summary table, then move the next month. Here is some sample code:

while ($weekarr=odbc_fetch_array($weekresult))
{

  $week1 = date('m.d.Y', strtotime($weekarr['weekstart']));
  $week2 = date('m.d.Y', strtotime($weekarr['weekend']));

  if ($weekarr['month']==$i)
   {


     echo "<table border='1' style='float: left' width=1 cellpadding='5'>";
     echo "<tr style='background-color:#484848;color:white'><td   colspan='4'>Week&nbsp;of&nbsp;".$week1."&nbsp;to&nbsp;".$week2;
     echo "<tr><td>Membership</td><td>Visitors</td><td>Purchased</td> <td>Comped</td></tr>";
   }//end if

  else
   { echo monthly table
     $i++;
   }
} //end while

After January ends (when month = 1), the first week of each new month is being skipped. How can I increase $i without skipping the first week of each month?

Upvotes: 0

Views: 73

Answers (1)

SGR
SGR

Reputation: 403

    while ($weekarr=odbc_fetch_array($weekresult))
    {

      $week1 = date('m.d.Y', strtotime($weekarr['weekstart']));
      $week2 = date('m.d.Y', strtotime($weekarr['weekend']));

      if ($weekarr['month']==$i)
       {


         echo "<table border='1' style='float: left' width=1 cellpadding='5'>";
         echo "<tr style='background-color:#484848;color:white'><td   colspan='4'>Week&nbsp;of&nbsp;".$week1."&nbsp;to&nbsp;".$week2;
         echo "<tr><td>Membership</td><td>Visitors</td><td>Purchased</td> <td>Comped</td></tr>";
       }//end if

      else
       { echo monthly table
         echo "<table border='1' style='float: left' width=1 cellpadding='5'>";
         echo "<tr style='background-color:#484848;color:white'><td   colspan='4'>Week&nbsp;of&nbsp;".$week1."&nbsp;to&nbsp;".$week2;
         echo "<tr><td>Membership</td><td>Visitors</td><td>Purchased</td> <td>Comped</td></tr>";
         $i++;
       }
    } //end while

Basically, you want to run to the output for the success condition for that one week in the fail condition

Upvotes: 3

Related Questions