如何按天分组数据库表数据并使用html表创建日历?

9wbgstp7  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(307)

我想做一个日历,显示每个月的每一天,并显示每天的事件信息。有时可能包含多个事件。问题是,我一直在尝试迭代结果集,并将多个事件分组到一个单日行中。
以下是一些示例行:

这是我检索数组的代码

$query="SELECT event_date_start, event_date_end, event_description, event_type, swim_club_assigned 
         FROM eventcalendar 
         WHERE event_date_start like '$search_date%'";
$result = mysqli_query($con, $query);

echo "<br>";

// Only loop if there is a result.
if ($result){
    $r = []; // You aren't exactly clear on 'myData'.  In this instance, I am setting the rows inside of it.
    while ($row = $result->fetch_object()) {
        $r[] = $row;
    }
    $result->close(); // Free result set
}

打印r($r);给我下面的数组

Array (
    [0] => stdClass Object (
        [event_date_start] => 2018-10-01
        [event_date_end] => 2018-10-01
        [event_description] => Preparation Meet (LC) - Template: A
        [event_type] => GCRSA Club Meet
        [swim_club_assigned] => Gold Coast North SC Inc
    )
    [1] => stdClass Object (
        [event_date_start] => 2018-10-11
        [event_date_end] => 2018-10-11
        [event_description] => Sprint Meet (LC) - Template: H
        [event_type] => GCRSA Club Meet
        [swim_club_assigned] => Bond SC Inc
    )
    [2] => stdClass Object (
        [event_date_start] => 2018-10-31
        [event_date_end] => 2018-10-31
        [event_description] => Reg Winter Championships (SC) - Template: XY
        [event_type] => GCRSA Championship
        [swim_club_assigned] => 
    )
)

我希望能够从数组中检索(其中事件类型= $something 开始日期= $a_date 然后显示 $swim_club 以及 $event_description .

while($row = fetch_assoc($r)) {
    if (($row["event_type"] == 'GCRSA Championship') && ($row["event_date_start"] == $day)){
        echo  "{$row['swim_club_assigned']} {$row['event_description']}";
    }
}

我一直试图找出如何从数组中提取每行的键数据(数组中每个开始日期可能有多个事件),但我无法找出从数组中检索行然后从键中提取值的语法。
要清楚的是,这是我试图填充的日历,结果将插入到相关日期行的单元格中。这就是为什么我认为最好在顶部使用一个查询,然后在每个单元格中遍历一个数组(如上面的示例所示),将事件的日期与相关单元格的日期相匹配。我正在使用的日历如下(带有注解以显示需要插入数据的单元格)

<table class="table table-bordered">
    <thead>
         <tr>
            <th rowspan="2" colspan="2">Date</th>
            <th colspan="3">GCRSA Region</th>
            <th colspan="2">State, SAL, Intl</th>
            <th colspan=2><center>Other events</center> </th>
         </tr>
         <tr style="border-bottom: 1pt Darkblue;">
            <th>Club meet</th>
            <th>Championship</th>
            <th>Development</th>
            <th>SQ</th>
            <th>Sal</th>
            <th>School Meets</th>
            <th>Qld School Holidays</th>
          </tr>
    </thead>
    <tbody>
        <?php
        // First day of the month.
        $date_start = date('Y-m-01', strtotime($query_date));
        $day = date('Y-m-01', strtotime($query_date));
        for($i= 1; $i < date('t', strtotime($query_date)) + 1; $i++){
        ?>
            <tr>
            <td <?php if (date('w',strtotime($day)) == 0 || date('w',strtotime($day)) == 6 ){
                echo "style='background-color:red;'";
            }?>><?php echo date('M d, Y',strtotime($day)); 

            ?></td>
            <td <?php if (date('w',strtotime($day)) == 0 || date('w',strtotime($day)) == 6 ){
                echo "style='background-color:red;'";
            }?>><?php echo date('l',strtotime($day)); ?></td>

            <td> <?php //this bit is not working. need to iterate through array result $r to filter appropriate events here 
            while($row = mysqli_fetch_assoc($r)) {
                if  (($row["event_type"] == 'GCRSA Club Meet') && ($row["event_date_start"] == $day)){
                    echo  "{$row['swim_club_assigned']} {$row['event_description']}";
                }
            }
            ?></td>

            <td> <?php //this bit is not working. need to iterate through array result $r to filter appropriate events here 
            while($row = mysqli_fetch_assoc($r)) {
                if  (($row["event_type"] == 'GCRSA Championship') && ($row["event_date_start"] == $day)){
                    echo  "{$row['swim_club_assigned']} {$row['event_description']}";
                }
            }
            ?></td>

        <?php
            for($j =1;$j<=5;$j++){
                echo "<td></td>".PHP_EOL;
            }
        ?>
        </tr>
        <?php 
        $day = date('Y-m-d', strtotime('+1 day', strtotime($day)));
        } 
        ?>                                    
    </tbody>
cwtwac6a

cwtwac6a1#

首先是代码段/文件,然后是解释和详细信息。。。

<style>
.table-bordered {border: solid 1px grey;}
thead tr th {border: solid 1px blue;}
tbody tr td {border: solid 1px green;}
.Sday {background-color: red;}
</style>
<?php
if (!$conn = new mysqli("localhost", "root","","db")) {
    echo "Database Connection Error"; // $conn->connect_error
} else {
    // Generate derived table comprised of united hardcoded dates for the current month with minimal function calls
    $ym = date('Y-m-');
    $derived_table = '';
    for ($d = 1, $end = date('t'); $d <= $end; ++$d) {
        $date = $ym . str_pad($d, 2, 0, STR_PAD_LEFT);  // format the date without calling strtotime() & date() each iteration
        if ($d == 1) {
            $derived_table .= "SELECT '$date' AS event_date_start";  // only need to name the first column
        } else {
            $derived_table .= " UNION SELECT '$date'";
        }
    }
    $query = "SELECT 
                DATE_FORMAT(A.event_date_start, '%b %e, %Y') AS Date,
                DAYNAME(A.event_date_start) AS Day,
                GROUP_CONCAT(
                  IF(event_type = 'GCRSA Club Meet', CONCAT(swim_club_assigned, ' ', event_description), null)
                  SEPARATOR '<br>'
                ) AS `Club Meet`,
                GROUP_CONCAT(
                  IF(event_type = 'GCRSA Championship', CONCAT(swim_club_assigned, ' ', event_description), null)
                  SEPARATOR '<br>'
                ) AS `Championship`
              FROM ($derived_table) A
              LEFT JOIN eventcalendar B ON A.event_date_start = B.event_date_start
              GROUP BY A.event_date_start";
    if (!$result = $conn->query($query)) {
        echo "Syntax Error"; // $conn->error
    } else {
        ?>
        <table class="table table-bordered">
            <thead>
                 <tr>
                    <th rowspan="2" colspan="2">Date</th>
                    <th colspan="3">GCRSA Region</th>
                    <th colspan="2">State, SAL, Intl</th>
                    <th colspan="2">Other events</th>
                 </tr>
                 <tr style="border-bottom: 1pt Darkblue;">
                    <th>Club meet</th>
                    <th>Championship</th>
                    <th>Development</th>
                    <th>SQ</th>
                    <th>Sal</th>
                    <th>School Meets</th>
                    <th>Qld School Holidays</th>
                  </tr>
            </thead>
            <tbody>     
                <?php
                while ($row = $result->fetch_assoc()) {
                    echo "<tr>";
                        echo "<td class=\"{$row['Day'][0]}day\">{$row['Date']}</td>";
                        echo "<td class=\"{$row['Day'][0]}day\">{$row['Day']}</td>";
                        echo "<td>{$row['Club Meet']}</td>";
                        echo "<td>{$row['Championship']}</td>";
                        echo "<td></td>";
                        echo "<td></td>";
                        echo "<td></td>";
                        echo "<td></td>";
                        echo "<td></td>";
                    echo "</tr>";
                }
                ?>
            </tbody>
        </table>
        <?php
    }
}

sql查询是这个脚本的主力。通过生成一个包含当前月份所有日期的“派生表”(按顺序),然后将实际的数据库表数据连接到该表并分组 event_date_start ,像这样的数据可以分组在一起,充分准备,并在一个紧凑的结果集中交付。
注意我的类/css技巧 {$row['Day'][0]}day <-的 [0] 表示“仅显示第一个字符。这使得类属性 Mday , Fday 为了避免条件检查,我将星期六和星期天分配给 Sday 所以造型很简单。
如果您想知道这个月呈现的查询是什么样子的:
派生表php演示
在开发此解决方案时,我没有添加未使用的列,但以下是我用来测试的数据:

CREATE TABLE IF NOT EXISTS `eventcalendar` (
  `event_description` varchar(255) NOT NULL,
  `event_date_start` date NOT NULL,
  `event_date_end` date NOT NULL,
  `event_type` varchar(255) NOT NULL,
  `swim_club_assigned` varchar(255) NOT NULL,
  PRIMARY KEY (`event_description`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `eventcalendar` (`event_description`, `event_date_start`, `event_date_end`, `event_type`, `swim_club_assigned`) VALUES
('Another Championship one!', '2018-10-11', '2018-10-11', 'GCRSA Championship', 'My Club 2'),
('Another one!', '2018-10-11', '2018-10-11', 'GCRSA Club Meet', 'My Club'),
('Dunno Invitational - Template: Z', '2018-11-12', '2018-11-12', 'GCRSA Championship', ''),
('Preparation Meet (LC) - Template: A', '2018-10-01', '2018-10-01', 'GCRSA Club Meet', 'Gold Coast North SC Inc'),
('Reg Winter Championships (SC) - Template: XY', '2018-10-31', '2018-10-31', 'GCRSA Championship', ''),
('Sprint Meet (LC) - Template: H', '2018-10-11', '2018-10-11', 'GCRSA Club Meet', 'Bond SC Inc');

它就是这样做的:

相关问题