2015-04-17 3 views
0

Я создаю календарь PHP, который связан с базой данных, содержащей список дат. Мне удалось создать календарь, подключиться к базе данных и отобразить даты. Однако вместо простого изменения класса, если дата найдена в базе данных, я получаю повторяющиеся даты.события отображения в календаре php

Мой код 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>";  

>

Это результат я получаю в моем браузере:

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

ответ

1

Я думаю, ваша потребность обработать $ доступной и $ обед, ужин $, $ fullday properly.I попытались change.Please проверить

<?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>"; 
0

Я не очень хорошо php.But Я думаю, что ваш код на

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

это эхо выводит все ваши class='day-cell available' dt.

Так что я думаю, что это эхо должно быть перенесено на последнее место в вашей петле while($row = mysql_fetch_assoc($result)).

Похоже

else if ($row['date_of_reservation'] == $datestring && $row['reservationType'] == "FULLDAY") 
      { 
       echo $fullday; 
      } 
else 
    { 
       echo $available; 
    } 
Смежные вопросы