PHP日历显示几个


PHP Calendar Display Several

我每周都在做(24小时日历),这将用于记录预订的时间和日期。

这就像一个时间表,里面有你预定的时段。为了确保已预订和未预订的时段是不同的,我在PHP if语句中放入时间,以对照数据库检查时间。

问题存在;我相信我的if语句只适用于一行,而while语句适用于多行。-所以基本上,我希望能够每天预订一个小时以上,并且每天看到一个以上的

(数据库插入不是问题-在日历上显示它是)

我不能做一个while语句,因为我不确定如何用它来构建我的时间表/日历。

这是我的一个问题:http://postimage.org/image/gcb3x3fk3/(如果我预订了9月9日18:00-它不会显示)

$globalid是一个全局变量,不包含在下面的文件/代码中(但包含在内)

我的代码

<div id="calendar">
<?
if(isset($_POST['add_week'])){
    $week = date('d-m-Y', strtotime($_POST['last_week']));
    $new_week =  strtotime ( '+1 week' , strtotime ( $week ) ) ;
    $new_week = date('d-m-Y', $new_week);

    $week_number = date("W", strtotime( $new_week));
    $year = date("Y", strtotime( $new_week));
}else if(isset($_POST['back_week'])){
    $week = date('d-m-Y', strtotime($_POST['last_week']));
    $new_week =  strtotime ( '-1 week' , strtotime ( $week ) ) ;
    $new_week = date('d-m-Y', $new_week);

    $week_number = date("W", strtotime( $new_week));
    $year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
/*if($week_number < 10){
   $week_number = "0".$week_number;
}*/
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
?>
<table name="week" width="100%" cellspacing="0" cellpadding="0" height="13">
    <tr>
<?
for($day=1; $day<=7; $day++)
{
?>
<tr>
<td class="day">
<?
$daycal = date('d M', strtotime($year."W".$week_number.$day))."";
echo $daycal;
?>
</td>
<td width="3"> </td>
<td>
<!-- Timetable -->
<table width="100%" cellspacing="0" cellpadding="0" height="13">
<tr><td>
<?
// Do not edit Below
$cal = mysql_query("SELECT * FROM `calendar` WHERE `day`='$daycal' && `userid`='$globalid'")or die(mysql_error());
$calendar = mysql_fetch_array($cal);
?>
<div class="hour" name="00:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="00:00"){ 
?>
Booked
<? }else{ ?>
00:00
<? } ?>
</div>
<div class="hour" name="01:00" id="<? echo $daycal; ?>">
<?
if($calendar2['time']=="01:00"){ 
?>
Booked
<? }else{ ?>
01:00
<? } ?>
</div>
<div class="hour" name="02:00" id="<? echo $daycal; ?>">
<?
if($calendar3['time']=="02:00"){ 
?>
Booked
<? }else{ ?>
02:00
<? } ?>
</div>
<div class="hour" name="03:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="03:00"){ 
?>
Booked
<? }else{ ?>
03:00
<? } ?>
</div>
<div class="hour" name="04:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="04:00"){ 
?>
Booked
<? }else{ ?>
04:00
<? } ?>
</div>
<div class="hour" name="05:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="05:00"){ 
?>
Booked
<? }else{ ?>
05:00
<? } ?>
</div>
<div class="hour" name="06:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="06:00"){ 
?>
Booked
<? }else{ ?>
06:00
<? } ?>
</div>
<div class="hour" name="07:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="07:00"){ 
?>
Booked
<? }else{ ?>
07:00
<? } ?>
</div>
<div class="hour" name="08:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="08:00"){ 
?>
Booked
<? }else{ ?>
08:00
<? } ?>
</div>
<div class="hour" name="09:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="09:00"){ 
?>
Booked
<? }else{ ?>
09:00
<? } ?>
</div>
<div class="hour" name="10:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="10:00"){ 
?>
Booked
<? }else{ ?>
10:00
<? } ?>
</div>
<div class="hour" name="11:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="11:00"){ 
?>
Booked
<? }else{ ?>
11:00
<? } ?>
</div>
<div class="hour" name="12:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="12:00"){ 
?>
Booked
<? }else{ ?>
12:00
<? } ?>
</div>
<div class="hour" name="13:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="13:00"){ 
?>
Booked
<? }else{ ?>
13:00
<? } ?>
</div>
<div class="hour" name="14:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="14:00"){ 
?>
Booked
<? }else{ ?>
14:00
<? } ?>
</div>
<div class="hour" name="15:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="15:00"){ 
?>
Booked
<? }else{ ?>
15:00
<? } ?>
</div>
<div class="hour" name="16:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="16:00"){ 
?>
Booked
<? }else{ ?>
16:00
<? } ?>
</div>
<div class="hour" name="17:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="17:00"){ 
?>
Booked
<? }else{ ?>
17:00
<? } ?>
</div>
<div class="hour" name="18:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="18:00"){ 
?>
Booked
<? }else{ ?>
18:00
<? } ?>
</div>
<div class="hour" name="19:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="19:00"){ 
?>
Booked
<? }else{ ?>
19:00
<? } ?>
</div>
<div class="hour" name="20:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="20:00"){ 
?>
Booked
<? }else{ ?>
20:00
<? } ?>
</div>
<div class="hour" name="21:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="21:00"){ 
?>
Booked
<? }else{ ?>
21:00
<? } ?>
</div>
<div class="hour" name="22:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="22:00"){ 
?>
Booked
<? }else{ ?>
22:00
<? } ?>
</div>
<div class="hour" name="23:00" id="<? echo $daycal; ?>">
<?
if($calendar['time']=="23:00"){ 
?>
Booked
<? }else{ ?>
23:00
<? } ?>
</div>
</td></tr>
</table>
<!-- End Timetable -->
</td>
</tr>
<tr height="3"><td> </td></tr>
<?
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
</div>

如此庞大而无用的代码。。为什么不循环通过和从100行代码只有10行?我试图重新组织你的代码。所以我终于有了这个:

<div id="calendar">
<?php
    $week = date('d-m-Y', strtotime($_POST['last_week']));
    if(isset($_POST['add_week']))
        $new_week =  strtotime ( '+1 week' , strtotime ( $week ) ) ;
    if(isset($_POST['back_week']))
        $new_week =  strtotime ( '-1 week' , strtotime ( $week ) ) ;
    $new_week = date('d-m-Y', $new_week);
    if(!isset($_POST['add_week']) && !isset($_POST['back_week']))
    {
        $week_number = date("W");
        $year = date("Y");
    }
    else
    {
        $week_number = date("W", strtotime( $new_week));
        $year = date("Y", strtotime( $new_week));
    }
    $week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
?>
<table name="week" width="100%" cellspacing="0" cellpadding="0" height="13">
    <tr>
<?
for($day=1; $day<=7; $day++)
{
?>
<tr>
    <td class="day">
        <?=$daycal = date('d M', strtotime($year."W".$week_number.$day))."";?>
    </td>
    <td width="3"> </td>
    <td>
    <!-- Timetable -->
    <table width="100%" cellspacing="0" cellpadding="0" height="13">
        <tr>
            <td>
            <?
                // Do not edit Below
                $cal = mysql_query("SELECT * FROM `calendar` WHERE `day`='$daycal' && `userid`='$globalid'")or die(mysql_error());
                $calendar = mysql_fetch_array($cal);
                for($cnt=0; $cnt<=24; $cnt++)
                { 
                    $hour = ($cnt<10?'0':'').$cnt.':00';
            ?>
                <div class="hour" name="<?=$hour?>" id="<? echo $daycal; ?>">
                    <?= ($calendar['time']==$hour) ? 'Booked' : $hour;  ?>
                </div>
            <? 
                } 
            ?>
            </td>
        </tr>
    </table>
    <!-- End Timetable -->
    </td>
</tr>
<tr height="3">
    <td> </td>
</tr>
<?
}
?>
</tr>
<tr>
    <form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
        <td colspan="7">
            <input type="submit" name="back_week" value="back_week" />
            <input type="submit" name="add_week" value="add_week" />
        </td>
    </form>
</tr>
</table>
</div>