foreach仅输出最后一次按日期查询结果,生产追踪器统计遇阻
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
$weekDatesand run your count query for that specific date, not justCURDATE(). - Double-check that your database's record date field (e.g.,
record_timestamp) is being converted to a date type for matching—useDATE(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.,
$keyvs$Key) or SQL field names (easy to mix uprecord_dateandrecord_timestamp).
内容的提问来源于stack exchange,提问作者Rath Baloth

