dtwk6019 2016-03-21 15: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 16: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

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

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要