简体   繁体   English

如何使用来自 php 数组的数据填充 html 表

[英]How to populate an html table with data from an array in php

I am new in php and I have problem to solve one task.我是 php 新手,我有解决一项任务的问题。

I have to create empty table for the school schedule for 7 hours (with the duration in 1 line) for Monday - Friday, 1 hour lasts 45 minutes (15 minute break, 2 break is 20 minutes, 5 break is 30 minutes).我必须为周一至周五的学校时间表创建 7 小时的空表(持续时间为 1 行),1 小时持续 45 分钟(15 分钟休息,2 休息是 20 分钟,5 休息是 30 分钟)。

I wrote this, but I don't know how to proceed.Can you please help me with this?我写了这个,但我不知道如何继续。你能帮我解决这个问题吗?

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html">
<title>Task1</title>
</head>
<body>            
<?php
header("Content-Type: text/html; charset=windows-1250");
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thurstday', 'Friday');
$times = array('08:00-08:45','09:00-09:45','10:05-10:50','11:05-11:50','12:05-12:50','13:20-14:05','14:20-15:05');
$rows = 5;
$columns = 7;
$i = 0;
$j = 0;

echo "<table border='1'>";
for ($i = 1; $i <= rows; $i++)
{
    echo("<tr>");
    for ($j = 1; $j <= columns; $j++)
        echo "<td>$days[$i]</td>";
    $i += 1;
    echo("</tr>");
}
echo("</table>");

You have several problems.你有几个问题。

  1. Array indexes start at 0 , not 1 .数组索引从0开始,而不是1 But it's usually clearer to use foreach .但通常使用foreach更清晰。
  2. You shouldn't hard-code the array lengths, use count() .您不应该对数组长度进行硬编码,请使用count()
  3. You're missing several $ before variable names.你在变量名之前缺少几个$
  4. You're not printing the times from $times .你不是从$times打印$times They should be printed as a header line before the first day.它们应该在第一天之前作为标题行打印。
  5. You shouldn't have $i = $i + 1;你不应该有$i = $i + 1; , as you'll increment $i twice because of $i++ . ,因为你会因为$i++而增加$i两次。
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thurstday', 'Friday');
$times = array('08:00-08:45','09:00-09:45','10:05-10:50','11:05-11:50','12:05-12:50','13:20-14:05','14:20-15:05');
$columns = count($times);

echo "<table border='1'>";
echo "<tr><th>Day</th>;";
foreach ($times as $time) {
    echo "<th>$time</th>";
}
echo "</tr>";
foreach ($days as $day) {
    echo("<tr><th>$day</th>");
    for ($i = 0; $i < $columns; $i++) {
        echo "<td></td>"; // empty fields for each period
    }
}
echo "</tr>";
echo("</table>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM