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

PHP读取CSV生成广播日程重复输出问题求助

广播节目日程PHP脚本重复输出问题排查与修复

问题描述

编写的PHP脚本用于读取CSV广播节目日程并按当前日期时间展示,但在周五14:00时,同时输出了13:00和14:00的节目行,出现重复输出问题。

原PHP脚本

<?php
// Step 1: Replace 'data.csv' with the path to your CSV file
$csvFile = 'schedule.csv';

// Step 2: Read CSV and store its contents in an array
if (($handle = fopen($csvFile, 'r')) !== false) {
    $csvData = [];
    while (($data = fgetcsv($handle)) !== false) {
        $csvData[] = $data;
    }
    fclose($handle);
} else {
    die("Error opening CSV file.");
}

// Step 3: Get the current day and time
$currentDay = date('l'); // l gives the full textual representation of the day (e.g., "Monday")
$currentHour = date('H'); // H gives the hour in 24-hour format (e.g., "13" for 1 PM)
// Adjust hour offset below.
$Hour = $currentHour+1;

// Step 4: Filter the array based on the current day and time
$filteredData = [];
foreach ($csvData as $row) {
    // Assuming that your CSV file has a "Day" column and a "Time" column
    $day = $row[0];
    $time = intval($row[1]);
$program_name= $row[2];
$program_when= $row[2];
$program_img= $row[4];

    // Check if the row matches the current day and time
   if ($day === $currentDay && $Hour >= $time) {
        $filteredData[] = $row;
    }
}

// Step 5: Display the filtered data with CSS classes for each row
if (count($filteredData) > 0) {


    foreach ($filteredData as $row) {
     

echo "<div class='row'>";
echo "<div class='col-5'>";
echo "<img src='$row[4]' width='90' height='90' border='0'>";
echo "</div>";
echo "<div class='col-7'>";
echo "<h3>$row[2]<br>$row[3]</h3>";
echo "</div>";
echo "</div>";
 
    }
} else {
    echo 'Not available.';
}

测试CSV数据

Friday,12,MMV,until 1pm,top_mmv.png
Friday,13,MMV,until 2pm,top_mmv.png
Friday,14,MMV,until 3pm,top_mmv.png
Friday,15,MMV,until 4pm,top_mmv.png

错误原因

  1. 过滤逻辑错误:脚本中定义$Hour = $currentHour+1,并使用$Hour >= $time作为筛选条件。当周五14:00时,$currentHour=14,$Hour=15,此时所有$time <=15的行都会被选中(即测试CSV中12、13、14、15点的节目),导致输出多个不符合预期的节目行。
  2. 冗余变量:代码中定义了$program_name、$program_when、$program_img但未实际使用,属于无效代码。

修复方案

根据需求不同,提供两种常见修复方式:

方式1:只显示当前正在播出的节目

如果需要展示当前时间对应的节目(如14:00-14:59显示14点的节目),修改筛选条件为匹配当前小时:

// Step 4: Filter the array based on the current day and time
$filteredData = [];
foreach ($csvData as $row) {
    $day = $row[0];
    $time = intval($row[1]);

    // 筛选当前日期且节目开始时间等于当前小时的行
    if ($day === $currentDay && $time == $currentHour) {
        $filteredData[] = $row;
    }
}

方式2:显示当前及之后的所有节目

如果需要展示当前时间之后(含当前)的全部节目,修改筛选条件为$time >= $currentHour:

// Step 4: Filter the array based on the current day and time
$filteredData = [];
foreach ($csvData as $row) {
    $day = $row[0];
    $time = intval($row[1]);

    // 筛选当前日期且节目开始时间大于等于当前小时的行
    if ($day === $currentDay && $time >= $currentHour) {
        $filteredData[] = $row;
    }
}

同时建议删除未使用的冗余变量,优化代码可读性。


内容的提问来源于stack exchange,提问作者Simon Parrock

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 23:00:00