You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

foreach仅输出最后一次按日期查询结果,生产追踪器统计遇阻

Troubleshooting Your Production Tracker's Shift/Date Stats Issue

Hey there, let's work through this stats bottleneck you're hitting with your production tracker. It sounds like you've got the core setup working, but the date range and shift counting logic is tripping you up. Let's break down the most likely issues and fixes:

1. Date Range Matching is Only Targeting Today (Sunday)

The fact that only Sunday shows non-zero counts tells me your query is probably hardcoded to match the current date instead of iterating through Monday to Sunday. Here's how to fix that:

  • First, generate a full week's worth of dates (Monday to Sunday) in your code. For example, in PHP:
    $weekDates = [];
    $startDate = strtotime("last Monday");
    for ($i = 0; $i < 7; $i++) {
        $weekDates[] = date("Y-m-d", $startDate);
        $startDate = strtotime("+1 day", $startDate);
    }
    
  • Then, loop through each date in $weekDates and run your count query for that specific date, not just CURDATE().
  • Double-check that your database's record date field (e.g., record_timestamp) is being converted to a date type for matching—use DATE(record_timestamp) = ? in SQL instead of comparing timestamps directly.

2. Night Shift Logic is Missing or Incorrect

Since only the day shift works, your night shift query is either missing entirely or doesn't account for cross-day shifts (a super common pitfall!). Most night shifts run from evening to early morning of the next day, so your query needs to cover both parts:
Example SQL for night shift counts (assuming night shift is 19:00–07:00, and counts belong to the shift's starting date):

SELECT COUNT(*) 
FROM production_records
WHERE 
  -- Part 1: Same day after 19:00
  (DATE(record_timestamp) = ? AND TIME(record_timestamp) >= '19:00:00')
  OR
  -- Part 2: Next day before 07:00
  (DATE(record_timestamp) = DATE_ADD(?, INTERVAL 1 DAY) AND TIME(record_timestamp) < '07:00:00');

If your night shift doesn't cross days, adjust the time range, but cross-day handling is usually the fix here.

3. Fixing the $key Output Issue

If $key isn't showing up correctly, it's likely a problem with how you're looping through your date data. Make sure you're capturing the date key in your loop:

foreach ($weekDates as $key => $dateStr) {
  // Run day/night shift counts for $dateStr
  echo "Date: {$key} ({$dateStr}) | Day Shift: {$dayCount} | Night Shift: {$nightCount}<br>";
}

Or if you're using an associative array with date strings as keys:

$stats = [
  '2024-05-20' => ['day' => 0, 'night' => 0],
  // ... other dates
];

foreach ($stats as $dateKey => $shiftCounts) {
  echo "{$dateKey}: Day = {$shiftCounts['day']}, Night = {$shiftCounts['night']}";
}

If $key still doesn't output, use var_dump($weekDates) or print_r($stats) to verify your array structure is correct—sometimes the key isn't set how you expect.

Quick Debugging Tips

  • Print out the full SQL query for each date/shift and run it directly in your database (e.g., phpMyAdmin, MySQL CLI). This will tell you if the problem is with your code or the query itself.
  • Verify you have test data for all days/shifts—empty results might just mean no records exist for those dates, not that your code is broken!
  • Check for typos in variable names (e.g., $key vs $Key) or SQL field names (easy to mix up record_date and record_timestamp).

内容的提问来源于stack exchange,提问作者Rath Baloth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:52:30