我正在尝试将我的mysql数据库中的数据检索到我从网上下载的php日历中。此代码的结果是页面不会停止加载。我知道它与循环有某种关系。如果在同一日期保存了一个事件,我想突出显示日期。
db connection//
班级日历{
public function __construct($year = '', $month = '') {
$date = time();
if (empty($year) OR empty($month)) {
$year = date('Y', $date);
$month = date('m', $date);
$day = date('d', $date);
}
$first_day = mktime(0, 0, 0, $month, 1, $year);
$title = date('F', $first_day);
$day_of_week = date('D', $first_day);
switch ($day_of_week) {
case "Mon": $blank = 0;
break;
case "Tue": $blank = 1;
break;
case "Wed": $blank = 2;
break;
case "Thu": $blank = 3;
break;
case "Fri": $blank = 4;
break;
case "Sat": $blank = 5;
break;
case "Sun": $blank = 6;
break;
}
$days_in_month = cal_days_in_month(0, $month, $year);
echo '<table width="100%" class="table table-striped">';
echo '<tr>';
echo '<th colspan=60>' . $title . ' ' . $year . '</th>';
echo '</tr>';
echo '<tr>';
echo '<td width=62>Mån</td>';
echo '<td width=62>Tis</td>';
echo '<td width=62>Ons</td>';
echo '<td width=62>Tors</td>';
echo '<td width=62>Fre</td>';
echo '<td width=62>Lör</td>';
echo '<td width=62>Sön</td>';
echo '</tr>';
$day_count = 1;
while ($blank > 0) {
echo '<td></td>';
$blank = $blank - 1;
$day_count++;
}
$day_num = 1;
$day_today = date('Y-m-d');
while ($day_num <= $days_in_month) {
$sql = "SELECT id, event_name, event_date FROM events WHERE event_date = '".$day_today."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if(date('d') == $event_date) {
echo "<td> <u>$day_num</u> </td>";
$day_num++;
$day_count++;
}
}
} else {
if(date('d') != $day_num) {
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
}
if(date('d') == $day_num) {
echo "<td> <strong>$day_num</strong> </td>";
$day_num++;
$day_count++;
}
if ($day_count > 7) {
echo '</tr><tr>';
$day_count = 1;
}
}
}
while ($day_count > 1 && $day_count <= 7) {
echo '<td> </td>';
$day_count++;
}
echo '</tr>';
echo '</table>';
}
}
$ c =新日历($ year,$ month);
答案 0 :(得分:0)
如果循环中的条件不适用,则$day_num
没有增加(并且因为$event_date
不存在而是这种情况)并且脚本将无限运行。
if (date('d') == $row["event_date"]) // instead of $event_date
另一方面,这个循环和这个条件对我来说似乎没什么必要。如果您只想标记当天,则$result->num_rows
上的检查就足够了。
if ($result->num_rows > 0)
{
echo "<td> <u>$day_num</u> </td>";
$day_num ++;
$day_count ++;
}
关于数据库查询,您当前始终请求同一天的数据,因为$day_today
始终是当天。你可以尝试这样的事情:
$check_date = $year ."-". $month ."-". $day_num;
$sql = "SELECT id, event_name, event_date FROM events
WHERE event_date = '" .$check_date . "'";
我希望这会对你有所帮助。