思路
涉及到多重數組,當用戶選擇那一個月,某一個客服的時候 客服 當月每天 以及總的資料查詢。
1.一個月內每天的時間戳生成數組:
for($i=0;$i<$j;$i++){ $date_array[] =array($start_time+$i*86400,$start_time+($i+1)*86400); //每隔一天賦值給數組 } $date_array[99]=array($thismonth_begin,$thismonth_end);
2.查詢這個月內有數據的客服:
$kefu[] = $db->query("select distinct author,count(author) as ca from $table where status=1 and part_id=2 and $at>=$lastmonth_end and $at<=$thismonth_end group by author order by ca ", "", "author");;
$kefu[2][99]="ALL";
ALL是所有客服一起的資料
可以用這個數組 循環生成單選列表
foreach ($kefu[2] as $k => $v){ echo "<option value='{$v}'"; if ($v==$kefus) { echo "selected=''"; } echo">{$v}</option>"; } ?>
3.數據查詢 ,循環遍歷時間戳數組,獲取開始和結束值
foreach ($date_array as $tname => $t) { $b = $t[0]; $e = $t[1]; $tname=$tname+1; // 每一天總潛在 // $data[$ptid][$tname+1]["dayall"] = $d1 = $db->query("select count(*) as c from $table where part_id=$ptid and $at>=$b and $at<=$e", 1, "c"); // 每天每個客服潛在: $data[$tname]["all"] = $d2 = $db->query("select count(*) as c from $table where $at>=$b and $at<=$e $pp", 1, "c"); // 每天每個客服預約: $data[$tname]["yuyue"] = $d3 = $db->query("select count(*) as c from $table where zhuanjia_num >1 $pp and $at>=$b and $at<=$e", 1, "c"); // 每天每個客服本地預約: $data[$tname]["byuyue"] = $db->query("select count(*) as c from $table where area='本市' and zhuanjia_num >1 $pp and $at>=$b and $at<=$e", 1, "c"); $data[$tname]["yiq"]= round($d5 / $d2 ,2)*100 ."%" ; $data[$tname]["yuq"]= round($d4 / $d2 ,2)*100 ."%"; // 已到: // $data[$ptid][$kf][$tname+1]["come"] = $d6 = $db->query("select count(*) as c from $table where and author='$kf' and $tf>=$b and $tf<=$e and status=1", 1, "c"); // 未到: // $data[$ptid][$kf][$tname+1]["leave"] = $d1 - $d2; // } // } }
4.判斷哪個客服 還是所有客服 根據GET獲取的 月份=》更新月份數組和 客服=》更新$pp的值 ,生成數據
if ( $kefus=='ALL') { $pp=""; }else{ $pp="and author='{$kefus}'"; }
5.用layui 前端展示數據
foreach($data as $k=>$v) { echo "<tr>"; echo "<td> $k </td>"; echo "<td>"; echo $v['all']; echo "</td>"; echo "<td>"; echo $v['yuyue']; echo "</td>"; echo "<td>"; echo $v['wyudao']; echo "</td>"; // } // 展示結束 echo "</tr>"; // } } ?>
總結 儘量用一個數組 搞定所有要展示的數據,一個數組可以 分項包含數組 以便更好展示
if ( $kefus=='ALL') { $pp=""; }else{ $pp="and author='{$kefus}'"; } echo "<br>"; foreach ($date_array as $tname => $t) { $b = $t[0]; $e = $t[1]; $tname=$tname+1; // 每一天總潛在 // $data[$ptid][$tname+1]["dayall"] = $d1 = $db->query("select count(*) as c from $table where part_id=$ptid and $at>=$b and $at<=$e", 1, "c"); // 每天每個客服潛在: $data[$tname]["all"] = $d2 = $db->query("select count(*) as c from $table where $at>=$b and $at<=$e $pp", 1, "c"); // 每天每個客服預約: $data[$tname]["yuyue"] = $d3 = $db->query("select count(*) as c from $table where zhuanjia_num >1 $pp and $at>=$b and $at<=$e", 1, "c"); // 每天每個客服本地預約: $data[$tname]["byuyue"] = $db->query("select count(*) as c from $table where area='本市' and zhuanjia_num >1 $pp and $at>=$b and $at<=$e", 1, "c"); $data[$tname]["wyuyue"] = $db->query("select count(*) as c from $table where area <> '本市' and zhuanjia_num >1 $pp and $at>=$b and $at<=$e", 1, "c"); // 每天每個客服預到: $data[$tname]["yudao"] = $d4 = $db->query("select count(*) as c from $table where $tf between $b and $e $pp ", 1, "c"); // 每天每個客服本地遇到: $data[$tname]["byudao"] = $db->query("select count(*) as c from $table where area='本市' and $tf between $b and $e $pp ", 1, "c"); $data[$tname]["wyudao"] = $db->query("select count(*) as c from $table where area <> '本市' and $tf between $b and $e $pp ", 1, "c"); // 每天每個客服已到: $data[$tname]["yidao"] = $d5 = $db->query("select count(*) as c from $table where status =1 and $tf between $b and $e $pp ", 1, "c"); // 每天每個客服本地已: $data[$tname]["byidao"] = $db->query("select count(*) as c from $table where status =1 and area='本市' and $tf between $b and $e $pp ", 1, "c"); $data[$tname]["wyidao"] = $db->query("select count(*) as c from $table where status =1 and area <> '本市' and $tf between $b and $e $pp ", 1, "c"); if ($d2 <=0) { $d2=1; } $data[$tname]["yiq"]= round($d5 / $d2 ,2)*100 ."%" ; $data[$tname]["yuq"]= round($d4 / $d2 ,2)*100 ."%"; // 已到: // $data[$ptid][$kf][$tname+1]["come"] = $d6 = $db->query("select count(*) as c from $table where and author='$kf' and $tf>=$b and $tf<=$e and status=1", 1, "c"); // 未到: // $data[$ptid][$kf][$tname+1]["leave"] = $d1 - $d2; // } // } }