Janani Nanayakkara Janani Nanayakkara - 1 month ago 4
HTML Question

How can I join 2 cells in a table while retrieving data from database from PHP?

I have to display time table (which store in the database-MySQL) in a web page using PHP. I can simply display it by using table. but one cell represent one hour.so for two hours lectures how can I merge 2 cells if adjacent cells include same subject? Please help me.
Further I should display those cells as text boxes and I should able to select those cells.
This is my code for display time table.

mysqli_select_db($connect,"timetable");

$result = mysqli_query($connect,"select * from 3yeariit2");


echo"<table border='1'>";
echo"<tr><th>Time</th>";
echo"<th>Monday</th>";
echo"<th>Tuesday</th>";
echo"<th>Wednesday</th>";
echo"<th>Thursday</th>";
echo"<th>Friday</th>";
echo"</tr>";

while($row = mysqli_fetch_array($result)){
$time = $row["time"];
$monday = $row["monday"];
$tuesday = $row["tuesday"];
$wednesday = $row["wednesday"];
$thursday = $row["thursday"];
$friday = $row["friday"];

echo"<tr><td>$time</td>";
echo"<td>$monday</td>";
echo"<td>$tuesday</td>";
echo"<td>$wednesday</td>";
echo"<td>$thursday</td>";
echo"<td>$friday</td>";
echo"</tr>";
}
echo"</table>";


For example among others, CST 391-2 [D1] should merge both 3.00-5.00 time slot in the image below

enter image description here

The method should be possible to apply in loading any other time table. Because I should display more time tables.

Answer

It seems you just added time as single string in database and columns for days. Where as the better way is creating start time and end time as separate columns as per below.

subjectcode | subjectname | start | end | ...

Then you can easily add 2 hours subject using one row.

But in this case you can check next related subject and if the subject is same do the html row span. Thereafter in next iteration you need to skip adding if particular subject was already printed using previous iteration.

I just explained the logic not the code. Hope it helps..

Comments