簡體   English   中英

查詢mysql表中具有相同月份值的行,並存儲為php數組,以備后用在日歷上顯示它們

[英]query a mysql table for rows that have the same month value and store as a php array for later use displaying them on a calendar

該主題基本上將其總結出來,但是我正在為客戶端的php開發日歷,該客戶端應該從表中提取事件並在適當的單元格中顯示它們。

日歷表沒有問題,但我要避免的是每月的每一天進行單獨的數據庫調用。

我試圖首先找到一種將結果存儲在數組數組中的方法,然后在創建日歷時,每天都有一個事件的事件將顯示該事件的某些數據,即使在那里也是如此。

這是我遇到的概念性問題,我似乎無法解決。

另外,不確定是否可行。

任何幫助表示贊賞。

謝謝。

例如:

select day, title, desc where month = $month and year = $year order by day asc

並最終輸出類似這樣的內容而不會命中數據庫31次:

day1.
title / desc


day2.
title / desc <br>
title / desc


day3. 
title / desc

您可以執行一次查詢以獲取此Mounth中的所有事件,

就像你寫

 select day, title, desc from mytable 
where month = $month and year = $year order by day asc

並在結果的foreach循環中,將其存儲在數組中,例如

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $events[$row['day']][] = $row;
}

因此,在循環結束時,您將獲得數組$ events,其中每個條目都包含當天的事件數組。

$events[1], $events[2] ....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM