简体   繁体   中英

display event in calendar php mysql

Can someone help me to debug my code? I'm creating a calendar that display the events with event_title and event_date that is stored on the database. The calendar displays the current month of the YEAR. I'm trying to query the events in the db, but the problem is that, nothing from the events show. Here's my code:

      <?php 
/* keep going with days.... */
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
        if($list_day == date('d') && $month == date('m') && $year == date('Y')) {
            $calendar.= '<div class = "today"><a href="event.php?&d='.$list_day.'&m='.$month.'&y='.$year.'">'."$list_day".'</a></div>';
        }else {
            /* add in the day number */
            $calendar.= '<div class="day-number"><a href="event.php?&d='.$list_day.'&m='.$month.'&y='.$year.'">'.$list_day.'</a></div>';
        }
        $event_day = $year.'-'.$month.'-'.$list_day;
        if(isset($events[$event_day])) {
            foreach($events[$event_day] as $event) {
                $calendar.= '<div class="event">'.$event['event_title'].'</div>';
            }
        }else {
            $calendar.= str_repeat('<p>&nbsp;</p>',2);
        }               
        $calendar.= '</div></td>';
        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr class="calendar-row">';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
    endfor;     
    /* get all events for the given month */
    $events = array();
    $query = "SELECT event_title, DATE_FORMAT(event_start,'%Y-%m-%d') AS event_start FROM calendar_events WHERE event_start LIKE '$year-$month%'";
    $result = mysql_query($query,$db_link) or die('cannot get results!');
    while($row = mysql_fetch_assoc($result)) {
        $events[$row['event_start']][]= $row;
    }

    ?>

Thanks in advance. Really need help.

Try to do:

<?php
// Create connection
$con = mysqli_connect("localhost","db_name","user","pass");

// Check connection
if (mysqli_connect_errno())
{
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT event_title, DATE_FORMAT(event_start,'%Y-%m-%d') AS event_start FROM calendar_events WHERE event_start LIKE '$year-$month%'";

$sql = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($sql))
{
    if (!$row)
    {
        echo 'Error';
        exit();
    }
    //Return of query
    echo $row['event_title'].$row['event_start'];
}
?>

Then you can save this results wherever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM