PHP时间表问题-While循环问题


PHP Timetable Issue - While Loop Issue

我正在制作一个简单的PHP时间表网站。我已经做了一张表,我有一个水平标题的时间,现在只有星期一,但我想增加5天,星期一到星期五。我只是不确定如何将正确的数据放在正确的时间和日期。

我的数据库有id、name、startTime、endTime和Day

一些数据可能是0、Basketball、9:15、10:15、Mon。

1,网球,10:15,11:15,周一。

2,板球,星期二11:15,12:15。

我目前有一个while循环,它可以填充表格,但不能填充正确的插槽(td)
这是我到目前为止的表格代码:

<?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'];
    if($start_time == "9:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "10:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "11:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "12:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "13:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "14:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "15:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "16:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "17:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}

echo "</tr>";

}
?>

有两件事。

你为什么有

<tr>
<th >Monday </th>   

这不是数据库馈送的,您需要将其自动化,并将其放在

如果您想动态打印

1,网球,10:15,11:15,周一。

您需要以这种方式对齐html。

<tr><th><?=$order?>,</th> <th><?=$sports?>,</th><th><?=$timing?></th><th><?=$day?></th></tr>

您还应该使用switch and case语句而不是if statements语句来占用更少的代码。

您还有$name2,它正在打印一个没有任何值分配给它的变量;也许您想使用$name来匹配td,看起来$name在代码中的值为$row['Name']

不要使用mysql_函数,它是不安全的和不推荐使用的,使用mysqli或pdo代替

此外,所有这些代码都可以更改为3行:

$times_array = array("9:15","11:15","12:15","13:15","14:15","15:15","16:15","17:15")
$row = in_array($start_time,$times_array)?$name2:"";
echo "<td>".$row."</td>";

==============================

if($start_time == "9:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "10:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "11:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "12:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "13:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "14:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "15:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "16:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}
if($start_time == "17:15"){
    echo"<td> $name2</td>";
} else {
    echo"<td> </td>";
}

使用数组和循环时,可以很容易地在php中创建日历/时间表。以下是我的做法。

首先,创建数组和时间-

//an array of all your start/end times
$times = array(
    array('start'=>'9:15', 'end'=>'10:15'),
    array('start'=>'10:15', 'end'=>'11:15'),
    array('start'=>'11:15', 'end'=>'12:15'),
    array('start'=>'12:15', 'end'=>'13:15'),
    array('start'=>'13:15', 'end'=>'14:15'),
    array('start'=>'14:15', 'end'=>'15:15'),
    array('start'=>'15:15', 'end'=>'16:15'),
    array('start'=>'16:15', 'end'=>'17:15'),
    array('start'=>'17:15', 'end'=>'18:15')
);

现在您可以使用循环构建您的标头

<table border="1px">
    <tr class="differentLine">
        <th >&nbsp;</th>
    <?php foreach($times as $time){
        echo '<th > '.$time['start'].' - '.$time['end'].' </th>';
    } ?>
    </tr >

其次,创建一个天数数组-

//use the short version (Mon, Tue, etc) as keys, as that is what you show in your data
//and use the long version for displaying
$days = array('Mon'=>'Monday', 'Tue'=>'Tuesday', 'Wed'=>'Wednesday', 'Thu'=>'Thursday', 'Fri'=>'Friday');

现在你可以使用循环来构建你的行

<?php foreach($days as $short=>$long){ ?>
    <tr>
        <th > <?php echo $long; ?> </th>
    </tr>
<?php } ?>

第三,创建一个数据库数据数组-

// order your database data by the Day and Start_Time
<?php
$sqlip = "Select * From Modules ORDER BY Day, Start_Time";
$query = mysql_query($sqlip) or die("Error");
// create an array to hold your data
$events = array();
// loop through your database rows and add to the array
while($row = mysql_fetch_array($query)){
    //add the row to the array, using the Day as a key and the Start_time as a key
    $events[$row['Day']][$row['Start_Time']] = $row;
}

现在你有了一个看起来像的阵列

$events = array(
    'Mon'=> array(
        '9:15' => array('id'=>0, 'name'=>'Basketball', 'startTime'=>'9:15', 'endTime'=>'10:15', 'Day'=>'Mon'),
        '10:15' => array('id'=>1, 'name'=>'Tennis', 'startTime'=>'10:15', 'endTime'=>'11:15', 'Day'=>'Mon')
    ),
    'Tue'=> array(
        '11:15' => array('id'=>2, 'name'=>'Cricket', 'startTime'=>'11:15', 'endTime'=>'12:15', 'Day'=>'Wed')
    )
);

第四,现在在您的日行循环中,循环遍历每个时间数组,并检查是否存在事件数组-

<?php foreach($days as $short=>$long){ ?>
    <tr>
        <th > <?php echo $long; ?> </th>
        <?php
        // check if there are any events for this day
        if(isset($events[$short])){
            //since there are events for this day, loop through all the times
            foreach($times as $k=>$time){
                //check if there is an event at this time
                if(isset($events[$short][$time['start']])){
                    //echo the event
                    echo '<td>'.$events[$short][$time['start']]['name'].'</td>';
                }
                else {
                    //since no event, echo blank cell
                    echo '<td></td>';
                }
            }
        }
        else {
            // since no events for the day, create blank cells for each time
            for($t=0;$t<count($times);$t++){
                echo '<td></td>';
        }
    } ?>
    </tr>';
<?php } ?>

现在,当你把所有这些放在一起时,它看起来就像

<?php 
//your database connection
...
//
//an array of all your start/end times
$times = array(
    array('start'=>'9:15', 'end'=>'10:15'),
    array('start'=>'10:15', 'end'=>'11:15'),
    array('start'=>'11:15', 'end'=>'12:15'),
    array('start'=>'12:15', 'end'=>'13:15'),
    array('start'=>'13:15', 'end'=>'14:15'),
    array('start'=>'14:15', 'end'=>'15:15'),
    array('start'=>'15:15', 'end'=>'16:15'),
    array('start'=>'16:15', 'end'=>'17:15'),
    array('start'=>'17:15', 'end'=>'18:15')
);
//use the short version (Mon, Tue, etc) as keys, as that is what you show in your data
//and use the long version for displaying
$days = array('Mon'=>'Monday', 'Tue'=>'Tuesday', 'Wed'=>'Wednesday', 'Thu'=>'Thursday', 'Fri'=>'Friday');
// order your database data by the Day and Start_Time
<?php
$sqlip = "Select * From Modules ORDER BY Day, Start_Time";
$query = mysql_query($sqlip) or die("Error");
// create an array to hold your data
$events = array();
// loop through your database rows and add to the array
while($row = mysql_fetch_array($query)){
    //add the row to the array, using the Day as a key and the Start_time as a key
    $events[$row['Day']][$row['Start_Time']] = $row;
} ?>
<table border="1px">
    <tr class="differentLine">
        <th >&nbsp;</th>
    <?php foreach($times as $time){
        echo '<th > '.$time['start'].' - '.$time['end'].' </th>';
    } ?>
    </tr >
<?php foreach($days as $short=>$long){ ?>
    <tr>
        <th > <?php echo $long; ?> </th>
        <?php
        // check if there are any events for this day
        if(isset($events[$short])){
            //since there are events for this day, loop through all the times
            foreach($times as $k=>$time){
                //check if there is an event at this time
                if(isset($events[$short][$time['start']])){
                    //echo the event
                    echo '<td>'.$events[$short][$time['start']]['name'].'</td>';
                }
                else {
                    //since no event, echo blank cell
                    echo '<td></td>';
                }
            }
        }
        else {
            // since no events for the day, create blank cells for each time
            for($t=0;$t<count($times);$t++){
                echo '<td></td>';
        }
    } ?>
    </tr>';
<?php } ?>