简体   繁体   English

在日历php mysql中显示事件

[英]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. 我正在创建一个日历,以显示带有存储在数据库中的event_title和event_date的事件。 The calendar displays the current month of the YEAR. 日历显示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. 然后,您可以将结果保存在任何地方。

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

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