dpd3982 2018-02-18 14:49
浏览 66

foreach仅按日期输出上次运行查询

I'm currently working on a small production tracker to make my life easier at work, all going well so far but i'm hitting a wall when it comes to getting some statistics.

I'm trying to count all rows with monday-sundays dates and between certain hours to denote day/night shifts.

Below is what I have so far, the issue I'm having is the only query that seems to be working is Days and even that seems to only want to output Sundays (today) count, everything else is coming back as 0. echoing the $key or $value foreach comes out correct though.

Thanks for any help.

$this_week = array($monday=>"monday", $tuesday=>"tuesday", $wednesday=>"wednesday", $thursday=>"thursday", $friday=>"friday", $saturday=>"saturday", $sunday=>"sunday");

foreach ($this_week as $key => $value) {

// Days

$sql_day = "SELECT count(*) FROM `stage_tbl` WHERE stage_1_date = :datenow AND stage_1_time BETWEEN :day_start AND :day_end"; 
$result_day = $conn->prepare($sql_day); 
$result_day->execute(array ('datenow' => $key, 'day_start' => $day_start, 'day_end' => $day_end)); 
$number_of_rows_day = $result_day->fetchColumn();

$day = "{$value}_day";

$$day = $number_of_rows_day;


// Nights

$datenext = date('Y-m-d', strtotime($key. ' + 1 days'));

$sql_night = "SELECT count(*) FROM `stage_tbl` WHERE stage_1_date = :datenow AND stage_1_time >= :night_start OR WHERE stage_1_date = :datenext AND stage_1_time <= :night_end"; 
$result_night = $conn->prepare($sql_night); 
$result_night->execute(array ('datenow' => $key, 'night_start' => $night_start, 'night_end' => $night_end, 'datenext' => $datenext)); 
$number_of_rows_night = $result_night->fetchColumn();

$night = "{$value}_night";

$$night = $number_of_rows_night;

}
  • 写回答

2条回答 默认 最新

  • douduidui1046 2018-02-18 15:27
    关注

    You don't need the variable $monday till $sunday keys. You are not assigning these variables.

    What you would want is this:

    $this_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    

    Notice that we also don't use the older array() syntax anymore to create an array. Nowadays we use the [] syntax.

    Now we can loop through:

    foreach ($this_week as $day) {
    
    // your logic goes here
    // $day will equal 'Monday' for the first run 
    // and 'Sunday' for the last run
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用