查询 mysql 表中具有相同月份值的行并将其存储为 php 数组以供以后使用在日历上显示它们

发布于 2024-09-26 14:23:44 字数 517 浏览 0 评论 0原文

这个主题基本上概括了它,但我正在为一个客户端开发一个 php 日历,它应该从表中提取事件并将它们显示在适当的单元格中。

日历表工作正常,但我试图避免的是为该月的每一天进行单独的数据库调用。

我试图找出一种方法,首先将结果存储在数组数组中,然后在创建日历时,每天有事件的日子都会显示该事件的部分数据。

这是我遇到的一个概念问题,我似乎无法解决它。

另外,不确定是否可能。

任何帮助表示赞赏。

谢谢。

例如:

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

最终输出类似这样的内容,而无需访问 DB 31 次:

day1.
title / desc


day2.
title / desc <br>
title / desc


day3. 
title / desc

the subject basically sums it up, but i am working on a calendar in php for a client that should pull events from a table and display them in the appropriate cells.

the calendar table is working no prob, but what i am trying to avoid is making a separate DB call for each day of the month.

i am trying to figure out a way to store the results in an array of arrays first, then as the calendar is created, each day that has an event(s) would display some portion of the data for that even there.

it's a conceptual problem i am having and i can't seem to solve it.

also, not sure if it's possible.

any help is appreciated.

thanks.

for example:

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

and ultimately output something like this without hitting the DB 31 times:

day1.
title / desc


day2.
title / desc <br>
title / desc


day3. 
title / desc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

决绝 2024-10-03 14:23:44

您可以执行一个查询来获取此挂载中的所有事件,

就像您

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

you can do one query to get all the events in this mounth,

like you write

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

and in the foreach loop on the results,
store it in array like

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

so in the end of the loop you have array $events, where every entry contain an array of the events in this day.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文