0

I am new in php and I have problem to solve one task.

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).

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>");
2
  • $rows != row ($ is missing) in for ($i = 1; $i <= rows; $i++), same for for ($j = 1; $j <= columns; $j++) Commented Jun 17, 2021 at 15:36
  • stackoverflow.com/questions/4977529/… that might help you a lot Commented Jun 17, 2021 at 15:38

1 Answer 1

1

You have several problems.

  1. Array indexes start at 0, not 1. But it's usually clearer to use foreach.
  2. You shouldn't hard-code the array lengths, use count().
  3. You're missing several $ before variable names.
  4. You're not printing the times from $times. They should be printed as a header line before the first day.
  5. You shouldn't have $i = $i + 1;, as you'll increment $i twice because of $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>");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it was very helpful @Barmar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.