我的日曆按照書面方式運作得很好,但是如果我在同一天新增另一個事件,它只會顯示資料庫中記錄的第一個事件。我需要能夠在同一天顯示多個活動。
我嘗試在程式碼中使用 for/next 迴圈和 while 迴圈來從資料庫中提取資訊。任何一種都無法按照程式碼的編寫方式運作。此連結將向您顯示所寫的工作日曆:Grims World 博客 這是我的程式碼:
<?php $cMonth = isset($_REQUEST["month"]) ? $cMonth = intval($_REQUEST["month"]) : $cMonth = date("m"); $cYear = isset($_REQUEST["year"]) ? $cYear = intval($_REQUEST["year"]) : $cYear = date("Y"); $prev_year = $cYear; $prev_year2 = $cYear-1; $next_year = $cYear; $next_year2 = $cYear+1; $prev_month = $cMonth-1; $next_month = $cMonth+1; if ($cMonth == 12 ) { $next_month = 1; $next_year = $next_year2; } elseif ($cMonth == 1 ) { $prev_month = 12; $prev_year = $prev_year2; } $short_days = array('1'=>'Sun', '2'=>'Mon', '3'=>'Tue', '4'=>'Wed', '5'=>'Thu', '6'=>'Fri', '7'=>'Sat'); $day=date('d'); $endDate=date('t',mktime(0,0,0,$cMonth,$day,$cYear)); echo "<table width='100%' align='center' border='0' cellpadding='0' cellspacing='5'><tr bgcolor='white'>\n"; echo "<td align='left'><a class='caldate3' href='".$_SERVER['PHP_SELF']."?month=$prev_month&year=$prev_year' title='Previous Month'> <big>««</big> Prev</a></td>\n"; echo "<td align='center'><a class='mcaldate2' href='#' title='Current Month'> ".date("F Y",strtotime($cYear."-".$cMonth."-01"))." </a></td>\n"; echo "<td align='right'><a class='caldate3' href='".$_SERVER['PHP_SELF']."?month=$next_month&year=$next_year' title='Next Month'>Next <big>»»</big> </a></td></tr>\n"; echo "<tr><td colspan='3' height='5'></td>\n"; echo "</tr></table>\n"; echo "<table width='100%' align='center' border='0' cellpadding='0' cellspacing='0'><tr><td class='norm'>\n"; echo "<table width='100%' align='center' border='0' cellpadding='2' cellspacing='1'><tr bgcolor='#000' height='20'>\n"; foreach ($short_days as $key=>$val) { echo "<td width='14%' align='center'><span style='font-size:14px; color:#ffff00;'><b>".$val."</b></span></td>\n"; } echo "</tr><tr>\n"; $s=date('w', mktime (0,0,0,$cMonth,1,$cYear)); for ($ds=1; $ds<=$s; $ds++) { echo "<td class='norm' height='20' align='center' valign='middle'></td>\n"; } for ($d=1; $d<=$endDate; $d++) { if (date('w',mktime (0,0,0,$cMonth,$d,$cYear)) == 0) { echo "</tr><tr>\n"; } $events = mysqli_query($connect, "SELECT * FROM `posts` WHERE SUBSTR(date,7,4)='$cYear' AND month='$cMonth' AND day='$d'"); $rows = mysqli_fetch_assoc($events); $post_id = $rows['id']; $post_title = $rows['title']; $evday = $rows['day']; if ($evday) { echo "<td height='20' class='event' align='center' valign='middle'>\n"; } elseif ($d == $day && $cMonth == date('m')) { echo "<td height='20' class='today' align='center' valign='middle'>\n"; } else { echo "<td height='20' class='norm' align='center' valign='middle'>\n"; } if ($d == $day && $cMonth == date('m') && $d <> $evday) { $d = str_pad($d,2,'0',STR_PAD_LEFT); echo "<span class='cal2'><a class='cal2' href='#' title='Today'>$d</a></span>\n"; } elseif ($d == $day && $cMonth == date('m') && $d == $evday) { $d = str_pad($d,2,'0',STR_PAD_LEFT); echo "<a class='cal2' href='post.php?id=$post_id' title='Today - $post_title'>$d</a>\n"; } elseif ($evday) { $d = str_pad($d,2,'0',STR_PAD_LEFT); echo "<a class='ecal' href='post.php?id=$post_id' title='$post_title'>$d</a>\n"; } else { $d = str_pad($d,2,'0',STR_PAD_LEFT); echo "<span class='noevt'>$d</span>\n"; } echo "</td>\n"; if (date('w',mktime (0,0,0,$cMonth,$d,$cYear)) == 6) { echo "</tr>"; } } echo "</table></td></tr><tr><td height='8'></td>\n"; echo "</tr></table>\n"; echo "</td></tr></table>\n"; ?>
P粉5614384072023-09-07 00:12:02
如果 SQL 查詢正確且 $events
傳回有效結果,請使用:
while($row = mysqli_fetch_assoc($events)) { # output events echo '<pre>'; print_r($row); echo '</pre>'; }
根據 php 文件,< code>mysqli_fetch_assoc 函數從結果集中取得一行資料並將其作為關聯數組傳回。對該函數的每次後續呼叫都會傳回結果集中的下一行(如果沒有更多行,則傳回 null),因此我們需要使用 while()
迴圈來循環結果集。 (雖然有結果......這樣做)。
要進一步改進程式碼,您可以先使用 mysqli_num_rows
內建函數檢查是否有任何結果。如果給定的日曆日期沒有任何事件,我們不需要循環結果,因此我們可以跳過它。
您的程式碼可能類似於:
$sql = "SELECT * FROM posts WHERE SUBSTR(date,7,4)='$cYear' AND month='$cMonth' AND day='$d' "; $results = mysqli_query($connect, $sql); if(mysqli_num_rows($results)) // only if there are results { while($row = mysqli_fetch_assoc($results)) { $post_id = $row['id']; $post_title = $row['title']; // ... # output echo '<p>'.$post_title.'</p>'; } }
注意 - 我已將您的$events
變數更改為$results
,只是為了一般最佳實踐目的和一致性,但從技術上來說這是這樣沒有必要。
另外 - 考慮在 SQL 中使用 ORDER BY
子句 (文件),以便在結果集中按時間順序對事件進行排序。
另外 - 考慮使用準備好的語句< /a> 用於您的 SQL 查詢,這更安全。