简体   繁体   English

在php日历中显示事件

[英]display events in a php calendar

I am building a PHP calendar, which is connected to a database containing a list of dates. 我正在构建一个PHP日历,该日历连接到包含日期列表的数据库。 I have managed to build the calendar, connect to the database, and display the dates. 我设法建立了日历,连接到数据库,并显示了日期。 However instead of simply changing the class of the if the date is found in the database, I am getting duplicate dates. 但是,我得到的不是重复的日期,而是简单地更改数据库中找到日期的类。

My PHP code: 我的PHP代码:

<?php
/* program name: Build Calender */
/* description: build calender */
/* 1 -> build table */
/* 2 -> add headings */
/* 3 -> add blanks */
/* 4 -> connect to db */
/* 5 -> check if dates in db = dates in calendar */
/* 6 ->  td with diff id */
/* 7 -> add remaining blanks */

$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));
$year =  (int) (isset($_GET['year']) ? $_GET['year'] : date('Y'));

echo "<h1 style='float:left; padding-right:30px;'>".date("F",mktime(0,0,0,$month,1,$year))." ".$year."</h1>";

$next = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next</a>';

/* "previous month" control */
$previous = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control">Previous </a>';

$controls = $previous."|".$next;
echo $controls;

$headings = array(0 =>'mon','tue','wed','thurs','fri','sat','sun');
$running_day = date('w',mktime(0,0,0,$month,0,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 1;
$dates_array = array();
$gourmet_type = "";

echo"<table border='1px'>\n";
for ($weeks = 0; $weeks<= 5 ; $weeks++)
{
    echo "<tr>\n";
    for ($days = 0; $days <= 6 ; $days++)
    {
        if($weeks == 0)
        {
            echo"<td>".$headings[$days]."</td>\n";  
        }
        else if ($weeks == 1 && $days < $running_day)  //Generates first row
        {
            echo "<td class='day-cell'>"."yy"."</td>\n";
        }
        else if ($day_counter <= $days_in_month)  //Adds the rest of the days
        {
        // varialbes for day-cell //
            $available  = "<td class='day-cell available' id='$day_counter'>".$day_counter."</td>\n";
            $lunch = "<td class='day-cell lunch' id='$day_counter'>".$day_counter."</td>\n";
            $dinner = "<td class='day-cell dinner' id='$day_counter'>".$day_counter."</td>\n";
            $fullday = "<td class='day-cell fullday' id='$day_counter'>".$day_counter."</td>\n";
            echo $available;
            $datestring = $day_counter.".".$month.".".$year;
            /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
            $db_link = mysql_connect("$host", "$user", "$password") or die('Cannot connect');
            mysql_select_db("$database",$db_link) or die('Cannot select the DB');
            $query = "SELECT * FROM reservations";
            $result = mysql_query($query,$db_link) or die('cannot get results!');
            while($row = mysql_fetch_assoc($result))
            {
            if  ($row['date_of_reservation'] == $datestring && $row['reservationType'] == "LUNCH")
                { 
                    echo $lunch;                     
                }
                else if ($row['date_of_reservation'] ==  $datestring &&  $row['reservationType'] == "DINNER")
                {
                    echo $dinner;
                }
                else if ($row['date_of_reservation'] == $datestring && $row['reservationType'] == "FULLDAY")
                {
                    echo $fullday;
                }
            }
            $day_counter++;
            }
        else  //Adds the blanks at the end
        {
            echo "<td class='day-cell'>"."yy"."</td>\n";
        }
    }
    echo "</tr>\n"; 
}
echo "</table>";    

?> ?>

This is the result I'm getting in my browser: 这是我在浏览器中得到的结果:

<h1 style='float:left; padding-right:30px;'>April 2015</h1><a href="?month=3&year=2015" class="control">Previous </a>|<a href="?month=5&year=2015" class="control">Next</a>
<table border='1px'>
<tr>
   <td>mon</td>
   <td>tue</td>
   <td>wed</td>
   <td>thurs</td>
   <td>fri</td>
   <td>sat</td>
   <td>sun</td>
</tr>
<tr>
   <td class='day-cell'>yy</td>
   <td class='day-cell'>yy</td>
   <td class='day-cell available' id='1'>1</td> <!--should hide -->
   <td class='day-cell dinner' id='1'>1</td>
   <td class='day-cell available' id='2'>2</td>
   <td class='day-cell available' id='3'>3</td>
   <td class='day-cell available' id='4'>4</td>
   <td class='day-cell available' id='5'>5</td> <!--should hide -->
   <td class='day-cell lunch' id='5'>5</td>  
</tr>
<tr>
    <td class='day-cell available' id='6'>6</td>
    <td class='day-cell available' id='7'>7</td>
    <td class='day-cell available' id='8'>8</td>
    <td class='day-cell available' id='9'>9</td>
    <td class='day-cell fullday' id='9'>9</td>
    <td class='day-cell available' id='10'>10</td>
    <td class='day-cell available' id='11'>11</td>
    <td class='day-cell available' id='12'>12</td>
</tr>
<tr>
     <td class='day-cell available' id='13'>13</td>
     <td class='day-cell available' id='14'>14</td>
     <td class='day-cell available' id='15'>15</td>
     <td class='day-cell available' id='16'>16</td>
     <td class='day-cell available' id='17'>17</td> <!--should hide -->
     <td class='day-cell lunch' id='17'>17</td>
     <td class='day-cell available' id='18'>18</td> <!--should hide -->
     <td class='day-cell dinner' id='18'>18</td>
     <td class='day-cell available' id='19'>19</td> <!--should hide -->
     <td class='day-cell lunch' id='19'>19</td>
 </tr>
 <tr>
      <td class='day-cell available' id='20'>20</td>
      <td class='day-cell available' id='21'>21</td> <!--should hide -->
      <td class='day-cell lunch' id='21'>21</td>
      <td class='day-cell available' id='22'>22</td>
      <td class='day-cell available' id='23'>23</td>
      <td class='day-cell available' id='24'>24</td>
      <td class='day-cell available' id='25'>25</td>
      <td class='day-cell available' id='26'>26</td>
 </tr>
 <tr>
      <td class='day-cell available' id='27'>27</td>
      <td class='day-cell available' id='28'>28</td>
      <td class='day-cell available' id='29'>29</td>
      <td class='day-cell available' id='30'>30</td> <!--should hide -->
      <td class='day-cell lunch' id='30'>30</td>
      <td class='day-cell'>yy</td>
      <td class='day-cell'>yy</td>
      <td class='day-cell'>yy</td>
 </tr>
 </table>

I think your need to handle $available and $lunch,$dinner,$fullday properly.I have tried to change.Please check 我认为您需要正确处理$ available和$ lunch,$ dinner,$ fullday。我已尝试更改。请检查

<?php
    /* program name: Build Calender */
    /* description: build calender */
    /* 1 -> build table */
    /* 2 -> add headings */
    /* 3 -> add blanks */
    /* 4 -> connect to db */
    /* 5 -> check if dates in db = dates in calendar */
    /* 6 ->  td with diff id */
    /* 7 -> add remaining blanks */

    $month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));
    $year =  (int) (isset($_GET['year']) ? $_GET['year'] : date('Y'));

    echo "<h1 style='float:left; padding-right:30px;'>".date("F",mktime(0,0,0,$month,1,$year))." ".$year."</h1>";

    $next = '<a href="?month='.($month != 12 ? $month + 1 : 1).'&year='.($month != 12 ? $year : $year + 1).'" class="control">Next</a>';

    /* "previous month" control */
    $previous = '<a href="?month='.($month != 1 ? $month - 1 : 12).'&year='.($month != 1 ? $year : $year - 1).'" class="control">Previous </a>';

    $controls = $previous."|".$next;
    echo $controls;

    $headings = array(0 =>'mon','tue','wed','thurs','fri','sat','sun');
    $running_day = date('w',mktime(0,0,0,$month,0,$year));
    $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
    $days_in_this_week = 1;
    $day_counter = 1;
    $dates_array = array();
    $gourmet_type = "";

    echo"<table border='1px'>\n";
    for ($weeks = 0; $weeks<= 5 ; $weeks++)
    {
        echo "<tr>\n";
        for ($days = 0; $days <= 6 ; $days++)
        {
            if($weeks == 0)
            {
                echo"<td>".$headings[$days]."</td>\n";  
            }
            else if ($weeks == 1 && $days < $running_day)  //Generates first row
            {
                echo "<td class='day-cell'>"."yy"."</td>\n";
            }
            else if ($day_counter <= $days_in_month)  //Adds the rest of the days
            {
            // varialbes for day-cell //
                $available  = "<td class='day-cell available' id='$day_counter'>".$day_counter."</td>\n";
                $lunch = "<td class='day-cell lunch' id='$day_counter'>".$day_counter."</td>\n";
                $dinner = "<td class='day-cell dinner' id='$day_counter'>".$day_counter."</td>\n";
                $fullday = "<td class='day-cell fullday' id='$day_counter'>".$day_counter."</td>\n";
                //echo $available;
                $toshow="";
                $datestring = $day_counter.".".$month.".".$year;
                /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
                $db_link = mysql_connect("$host", "$user", "$password") or die('Cannot connect');
                mysql_select_db("$database",$db_link) or die('Cannot select the DB');
                $query = "SELECT * FROM reservations";
                $result = mysql_query($query,$db_link) or die('cannot get results!');
                while($row = mysql_fetch_assoc($result))
                {
                if  ($row['date_of_reservation'] == $datestring && $row['reservationType'] == "LUNCH")
                    { 
                       $toshow=$lunch;                     
                    }
                    else if ($row['date_of_reservation'] ==  $datestring &&  $row['reservationType'] == "DINNER")
                    {
                        $toshow=$dinner;
                    }
                    else if ($row['date_of_reservation'] == $datestring && $row['reservationType'] == "FULLDAY")
                    {
                       $toshow=$fullday;
                    }

                }

                if($toshow==""){
                echo $available;
                }
                else{
                echo $toshow;
                }

                $day_counter++;
                }
            else  //Adds the blanks at the end
            {
                echo "<td class='day-cell'>"."yy"."</td>\n";
            }
        }
        echo "</tr>\n"; 
    }
    echo "</table>";

I'm Not very good at php.But I think your code at 我不太擅长php。但是我认为您的代码在

$fullday = "<td class='day-cell fullday' id='$day_counter'>".$day_counter."</td>\n";
echo $available;

this echo will output all of your class='day-cell available' dt. 此回显将输出您所有的class='day-cell available'

So i think this echo should be moved to the last else in your while($row = mysql_fetch_assoc($result)) loop. 所以我认为这个回声应该在while($row = mysql_fetch_assoc($result))循环中移到最后一个。

Looks like 看起来像

else if ($row['date_of_reservation'] == $datestring && $row['reservationType'] == "FULLDAY")
            {
                echo $fullday;
            }
else 
     {
                echo $available;
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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