我正在制作一个简单的PHP时间表网站。我已经制作了一张桌子,我的时间处于横向标题,目前只是星期一,但我希望在星期一到星期五增加5天。我不确定如何在正确的时间和日期输入正确的数据。
我的数据库有id,name,startTime,endTime和Day
有些数据可能是0,篮球,9:15,10:15,周一。
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 > </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>";
}
?>
答案 0 :(得分:0)
有几件事。
为什么有
<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&#39;,看起来$name
在您的代码中的值为$row['Name']
。
答案 1 :(得分:0)
使用数组和循环时,可以在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 > </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 > </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 } ?>
答案 2 :(得分:0)
不要使用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>";
}