简体   繁体   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

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. 该主题基本上将其总结出来,但是我正在为客户端的php开发日历,该客户端应该从表中提取事件并在适当的单元格中显示它们。

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: 并最终输出类似这样的内容而不会命中数据库31次:

day1.
title / desc


day2.
title / desc <br>
title / desc


day3. 
title / desc

you can do one query to get all the events in this mounth, 您可以执行一次查询以获取此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 并在结果的foreach循环中,将其存储在数组中,例如

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. 因此,在循环结束时,您将获得数组$ events,其中每个条目都包含当天的事件数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP-如何在数组中存储数组键并在以后使用它访问值 - PHP - How to store an arrays keys in an array and use it to later access the value 如何存储随机MySQL查询以供以后使用? - How to store random MySQL query for later use? PHP SQL Joomla将具有两列的表存储到数组中,并在以后回显它们 - PHP SQL Joomla store table with two columns into array and echo them later MYSQL连接表行并用于php数组 - MYSQL Join table rows and use for php array PHP:将数组输出存储在变量中,以备后用 - PHP: Store array output in a variable for later use (PHP)数组中的键值对数目未知,如何在sql查询中使用它们? - (PHP) I have an unknown number of key value pairs in an array, how can I use them in an sql query? 查询MySQL表,存储在PHP数组中,然后从数组中返回单个值 - Query a MySQL Table, store in PHP array, then return a single value from the array 如何使用PHP查询MySQL的某些行,然后针对每一行,基于一个UID,基于值查询另一个表? - How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value? PHP存储循环中的所有变量以供以后使用 - PHP to store all variables from loop to use them later 从 mysql 表中获取数据并在 sql 查询中的“IN”中使用它们 (PHP) - Get data from mysql table and use them in "IN" in sql query (PHP)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM