search

Home  >  Q&A  >  body text

How to display multiple events per day in my php calendar code

My calendar works fine as written, but if I add another event on the same day, it only displays the first event recorded in the database. I need to be able to display multiple events on the same day.

I tried using for/next loops and while loops in my code to extract information from the database. None of it works the way the code is written. This link will show you the work calendar as written: Grims World Blog This is my code:

 <?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";
?>

P粉950128819P粉950128819448 days ago628

reply all(1)I'll reply

  • P粉561438407

    P粉5614384072023-09-07 00:12:02

    If the SQL query is correct and $events returns valid results, use:

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

    According to the php documentation , the < code>mysqli_fetch_assoc function fetches a row of data from the result set and returns it as an associative array. Each subsequent call to this function will return the next row in the result set (or null if there are no more rows), so we need to use a while() loop to loop over the result set. (Although there are consequences...doing this).

    To improve the code further, you can first check if there are any results using the mysqli_num_rows built-in function. We don't need to loop over the results if there are no events for a given calendar date, so we can skip it.

    Your code might look like:

    $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>';
        }
    }

    NOTE - I have changed your $events variable to $results just for general best practice purposes and consistency, but from a technical perspective There is no need to say this is the case.

    Also - Consider using the ORDER BY clause in SQL (Documentation) to order events chronologically in the result set.
    Also - Consider using Prepared Statements< /a> for your SQL queries, which is safer.

    reply
    0
  • Cancelreply