从数据库到日历页面获取事件

时间:2016-04-15 17:12:17

标签: php mysql calendar

数据库: Database 日历1: Calendar 1 日历2: Calendar 2 日历3: Calendar 3

我正试图从数据库中获取这两个事件到星期一4号,我想知道为什么我只在四月和五月举办一个活动。

$sql = "SELECT title, contact, contact_email, DAYOFMONTH(start_date) 
        FROM $caltbl WHERE start_date LIKE '$year-$month%'";
$result = mysql_query($sql)or die(mysql_error());
$row_count = mysql_num_rows($result);
$record = mysql_fetch_array($result);

while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\">
      &nbsp;<a href='index.php?day=$day_num-$title-$year'>
      $day_num</a><p/>";

if ($day_num == $record['DAYOFMONTH(start_date)']){     
    echo "&nbsp;<a href=\"#\">".$record['title']. "</a><br/>";      
} else{
    echo "<br />&nbsp;" . "<br/>&nbsp;";
}
echo "</td>";
$day_num++;
$day_count++;

编辑:

while ($day_num <= $days_in_month) {
echo "<td width=\"42\" valign=\"top\">&nbsp;<a href='index.php?day=$day_num-$title-$year'>$day_num</a><p/>";

if ($day_num == $record['DAYOFMONTH(start_date)']){

    while ($event = mysql_fetch_assoc($result)) {
        $array[] = $event;
    }
    print_r($array);
    echo "&nbsp;<a href=\"#\">".$event['title']. "</a><br/>";

} else{
    echo "<br />&nbsp;" . "<br/>&nbsp;";
}
echo "</td>";
$day_num++;
$day_count++;

// Make sure we start a new row each week
if ($day_count > 7) {
    echo "</tr><tr>";
    $day_count = 1;
}

}

1 个答案:

答案 0 :(得分:0)

打开一个空白数组:

$arr = array();        
 while ($event = mysql_fetch_assoc($result)) {
        $arr[] = $event;//store all the data in the $arr array
    }

运行foreach循环以打印事件详细信息:

foreach($arr as $k=>$v){
    if($v['DATEOFMONTH(start_date)']==$day_num){
      echo '<a href="#">'.$v['title'].'</a><br/>';
    }else{
        echo "<br />&nbsp;" . "<br/>&nbsp;";
    }
  }

希望这可能会有所帮助。 demo