如何在我的php日历代码中显示每天多个事件

tmb3ates  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(100)

我的日历工作漂亮,因为写,但如果我添加另一个事件的同一天,它只会显示第一个事件记录在数据库中。我需要能够显示同一天的多个事件。
我尝试在代码中使用for/next循环和while循环从数据库中提取信息。任何一个都不能与代码的编写方式一起工作。此链接将向您显示所写的工作日历:Grims World Blog这是我的代码:

<?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&amp;year=$prev_year' title='Previous Month'>&nbsp;<big>&laquo;&laquo;</big>&nbsp;Prev</a></td>\n";
echo "<td align='center'><a class='mcaldate2' href='#' title='Current Month'>&nbsp;".date("F Y",strtotime($cYear."-".$cMonth."-01"))."&nbsp;</a></td>\n";
echo "<td align='right'><a class='caldate3' href='".$_SERVER['PHP_SELF']."?month=$next_month&amp;year=$next_year' title='Next Month'>Next&nbsp;<big>&raquo;&raquo;</big>&nbsp;</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";
?>

字符串

bz4sfanl

bz4sfanl1#

如果SQL查询是正确的,并且$events返回有效结果,则用途:

while($row = mysqli_fetch_assoc($events))
{
    # output events
    echo '<pre>';
    print_r($row);
    echo '</pre>';
}

字符串
按照php documentationmysqli_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(documentation)中使用ORDER BY子句,以便在结果集中按时间顺序对事件进行排序。
  • 另外 * -考虑使用Prepared Statements进行SQL查询,这更安全。

相关问题