关闭
dtwk6019 2016-03-21 07:54
浏览 53
已采纳

如何使用if / else语句迭代sql结果数组?

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?

展开全部

  • 写回答

1条回答 默认 最新

  • duanlie2709 2016-03-21 08:00
    关注
        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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部