PHP脚本读取CSV文件返回echo多次

qlckcl4x  于 2023-07-31  发布在  PHP
关注(0)|答案(3)|浏览(83)

我已经将一个广播时间表PHP脚本整合在一起,用于读取CSV文件,并根据时间和日期将结果发布在网站上。但是,它似乎发布了两次。
以下是脚本:

<?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


但是当它在周五到了下午2点的时候,它会同时发布下午1点的那一行和下午2点的那一行。我做错什么了,拜托?

bq3bfh9z

bq3bfh9z1#

您需要指定要收集数据的条件,

if ($day === $currentDay && $Hour >= $time) {

字符串
例如,现在的当前时间是14(+1小时是您的调整)
$小时>= $时间

15 >= time from first line of csv is 12 - true
15 >= time from second line of csv is 13 - true
15 >= time from third line of csv is 14 - true
15 >= 15 - true


我想你能看出哪里出了问题。

col17t5w

col17t5w2#

您向我们展示的代码实际上并没有演示您提到的问题?
但是,您可以简化代码并消除对2个数组的需求,并使代码更易于阅读和维护。

$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;

// The input file
$csvFile = 'schedule.csv';

// Step 2: Read CSV and process the output
if (($handle = fopen($csvFile, 'r')) !== false) {
    while (($row = fgetcsv($handle)) !== false) {
        $day = $row[0];
        $time = intval($row[1]);
    
        // Check if the row matches the current day and time
        if ($day === $currentDay && $Hour >= $time) {
            echo "<div class='row'>\n";
                echo "  <div class='col-5'>\n";
                    echo "      <img src='$row[4]' width='90' height='90' border='0'>\n";
                echo "  </div>\n";
                echo "  <div class='col-7'>\n";
                    echo "      <h3>$row[2]<br>$row[3]</h3>\n";
                echo "  </div>\n";
            echo "</div>\n";
        }
    }
    fclose($handle);
} else {
    die("Error opening CSV file.");
}

字符串
利用这些数据

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


并在14:00到14:59之间运行代码,输出为

<div class='row'>
  <div class='col-5'>
      <img src='top_mmv.png' width='90' height='90' border='0'>
  </div>
  <div class='col-7'>
      <h3>MMV<br>until 1pm</h3>
  </div>
</div>
<div class='row'>
  <div class='col-5'>
      <img src='top1_mmv.png' width='90' height='90' border='0'>
  </div>
  <div class='col-7'>
      <h3>MMV<br>until 2pm</h3>
  </div>
</div>
<div class='row'>
  <div class='col-5'>
      <img src='top2_mmv.png' width='90' height='90' border='0'>
  </div>
  <div class='col-7'>
      <h3>MMV<br>until 3pm</h3>
  </div>
</div>


有什么不对吗?

amrnrhlw

amrnrhlw3#

但是当它在周五到了下午2点的时候,它会同时发布下午1点的那一行和下午2点的那一行。我做错什么了,拜托?
从你的问题的日期,2023-07-28 13:21:42 Z,和(时区?)您应用的偏移量

$currentHour = date('H'); // H gives the hour in 24-hour format (e.g., "13" for 1 PM)
// Adjust hour offset below.
$Hour = $currentHour+1;

字符串
对于该条件,这是一个简单的计算错误:

$Hour >= $time


$time来自CSV数据,$Hour=14大于或等于12、13和14。因此,它将错误地显示为显示直到下午1点,但不再运行/计划。
因此,显而易见的解决方案是删除小时的偏移量,并与$time的一小时帧 end 时间进行比较:

$currentHour <= $time

相关问题