douqin6785 2012-09-08 09:48
浏览 62

PHP日历显示几个

I am doing a week-by-week (24 hour calendar), this is going to be used to record what hours have been booked and on what days.

It's like a timetable with slots that you book. To ensure that booked and unbooked slots were different I put in PHP if statements to check the time against the database.

Problem being; I believe my if statements are only working for one row unlike a while statement that works over several. - so basically I want to be able to book more than one hour per day - and see more than one hour per day

(the database insert isn't a problem - displaying it on the calendar is)

I cannot do a while statement as I am unsure how I would structure my schedule/calendar with one.

Here is an my problem: http://postimage.org/image/gcb3x3fk3/ (if I booked 18:00 on Sep 9 - it wouldn't show)

$globalid is a global variable and is not held within the file/code below (but is included)

MY CODE

<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>
  • 写回答

1条回答 默认 最新

  • drt12345678 2012-09-08 10:52
    关注

    So huge and useless code.. Why not to loop through and from 100 lines of code have only 10? I tried to reorganize your code. So finally i have this one:

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题