Member Avatar for anmol.raghuvanshi1
<?php 

$host = 'localhost';
$user = 'root';
$pass = '******';
$db = 'test';
$error = 'Error Connecting to database';
$error1 = 'Error Selecting to database';   
$connect = mysql_connect($host,$user,$pass) or die($error);
$select_db = mysql_select_db($db) or die($error1);

?>

<table border="1px">
<tr>
<div class="differentLine">
<th >&nbsp;</th>
<th > 9:15 - 10:15 </th>
<th > 10:15 - 11:15 </th>
<th > 11:15 - 12:15 </th>
<th > 12:15 - 13:15 </th>
<th > 13:15 - 14:15 </th>
<th > 14:15 - 15:15 </th>
<th > 15:15 - 16:15 </th>
<th > 16:15 - 17:15 </th>
<th > 17:15 - 18:15 </th>
</div>
</tr >

<tr>
<th >Monday </th>   


<?php
$sqlip = "Select * From Modules";
$query = mysql_query($sqlip) or die("Error");

while($row = mysql_fetch_array($query))
{
    $name = $row['Name'];
    $day = $row['Day'];
    $start_time = $row['Start_Time'];
    $End_Time = $row['End_Time'];

    echo "
    <td> $name</td>";



}

?>

I'm making a simple PHP timetabling website. I've made a table and I have the times in a horizontal header and days Monday to Friday in a vertical header. I'm just unsure of how to put the correct data in the correct times.

My database has id, name, startTime, endTime and Day

Some data could be 0, Basketball, 9:00, 10:00, Mon.

I currently have a while loop that can fill in the table, but not in the correct slots (td).
Here is my table code so far:

Member Avatar for diafol

If you have 09:00-10:00 as a time for an event in the DB, then why does your html table start 09:15 ? Sounds whacky.

Oh BTW - stop using mysql_* functions they are deprecated and have been for a considerable amount of time. Use mysqli or PDO.

Agreed with @diafol, If I am in the case, I will change the code to this:

<th >&nbsp;</th>
<th > <9:00 </th>
<th > 9:00 - 10:00 </th>
<th > 10:00 - 11:00 </th>
<th > 11:00 - 12:00 </th>
<th > 12:00 - 13:00 </th>
<th > 13:00 - 14:00 </th>
<th > 14:00 - 15:00 </th>
<th > 15:00 - 16:00 </th>
<th > 16:00 - 17:00 </th>
<th > 17:00 - 18:00 </th>
<th > >18:00 </th>

then I will create an array of times

$time_array = array(
    array(
        'Start'=>strtotime("0:00"),
        'End'=>strtotime("9:00")
    ),
    array(
        'Start'=>strtotime("9:00"),
        'End'=>strtotime("10:00")
    ),
    array(
        'Start'=>strtotime("10:00"),
        'End'=>strtotime("11:00")
    ),
    array(
        'Start'=>strtotime("11:00"),
        'End'=>strtotime("12:00")
    ),
    array(
        'Start'=>strtotime("12:00"),
        'End'=>strtotime("13:00")
    ),
    array(
        'Start'=>strtotime("13:00"),
        'End'=>strtotime("14:00")
    ),
    array(
        'Start'=>strtotime("14:00"),
        'End'=>strtotime("15:00")
    ),
    array(
        'Start'=>strtotime("15:00"),
        'End'=>strtotime("16:00")
    ),
    array(
        'Start'=>strtotime("16:00"),
        'End'=>strtotime("17:00")
    ),
    array(
        'Start'=>strtotime("17:00"),
        'End'=>strtotime("18:00")
    ),
    array(
        'Start'=>strtotime("18:00"),
        'End'=>strtotime("23:59")
    )
);

After that, I will change the $start_time and $End_Time into

    $start_time = strtotime($row['Start_Time']);
    $End_Time = strtotime($row['End_Time']);

finally, create array of tds and store the data with comparisn

$tds = array($row_num); // the $row_num represent the "0" in database as from what you showed
    for($i = 0; $i < count($time_array); $i++){
        $start = false;
        $end = false;
        if($start_time >= $time_array[$i]['Start']){
            $start = true;
        }
        if($End_Time <= $time_array[$i]['End']){
            $end = true;
        }
        if(($start_time >= $time_array[$i]['Start']) && ($End_Time <= $time_array[$i]['End'])){
            echo $start_time."<br/>";
            echo $End_Time."<br/>";
            $tds[$i + 1] =  $name;
        }else{
            $tds[$i + 1] =  "";
        }
    }
    echo "<tr>";
    foreach($tds as $td){
        echo "<td>$td</td>";
    }
    echo "</tr>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.