我正在构建一个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>";
&GT;
这是我在浏览器中得到的结果:
<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>
答案 0 :(得分:1)
我认为你需要处理$ 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>";
答案 1 :(得分: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;
}