在日历php mysql中显示事件

时间:2014-05-06 08:15:34

标签: php mysql events calendar

有人可以帮我调试我的代码吗?我正在创建一个日历,用于显示存储在数据库中的event_title和event_date的事件。日历显示YEAR的当前月份。我正在尝试查询数据库中的事件,但问题是,事件中没有任何内容显示。这是我的代码:

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

    ?>

提前致谢。真的需要帮助。

1 个答案:

答案 0 :(得分:0)

尝试:

<?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'];
}
?>

然后您可以将此结果保存在任何地方。