简体   繁体   中英

PHP Calendar Display Several

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM