连接到数据库mysql以获取事件的php日历

时间:2013-01-20 15:46:32

标签: php mysql

所以我有一个非常简单的日历代码,但是我无法弄清楚如何查询mysql数据库以查找每月的每一天的任何事件。我知道如何查询数据库中的信息,但我不知道我会把它放在创建日历的代码或如何构建它或任何东西。香港专业教育学院看了许多PHP日历的代码,并且对于每个代码我都无法从我的数据库中获取数据,我只是不知道该怎么做。任何帮助都很感激,谢谢。 日历代码编辑为包含db stuff:

include ("connection.php"); 

$date =time (); 
$day = date('d', $date); 
$month = date('m', $date); 
$month = $month + "02";
$year = date('Y', $date); 
$years = substr($year, 2, 2);

$first_day = mktime(0,0,0,$month, 1, $year); 
$title = date('F', $first_day); 
$day_of_week = date('D', $first_day); 
switch($day_of_week){ 
    case "Sun": $blank = 0; break; 
    case "Mon": $blank = 1; break; 
    case "Tue": $blank = 2; break; 
    case "Wed": $blank = 3; break; 
    case "Thu": $blank = 4; break; 
    case "Fri": $blank = 5; break; 
    case "Sat": $blank = 6; break; 
}

$days_in_month = cal_days_in_month(0, $month, $year);
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td 
width=42>T</td><td width=42>W</td><td width=42>T</td><td 
width=42>F</td><td width=42>S</td></tr>";
$day_count = 1;
echo "<tr>";

while ( $blank > 0 ) 
{ 
    echo "<td></td>"; 
    $blank = $blank-1; 
    $day_count++;
} 
$day_num = 1;
 while ( $day_num <= $days_in_month ) 
{ 

echo "<td> $day_num <br/>";


$result = mysql_query("SELECT time, length FROM hire WHERE day = '$day_num' and month =    '$month' and year = '$years'") or die ('Error: '.mysql_error ());

while($ row = mysql_fetch_array($ result)){

$time = $row['time'];
$length = $row['length'];

}

if (isset($time) and (isset($length))) {
 echo "Time: " . $time . "<br/> Length: " . $length . "<br/>";
}

"</td>";



    $day_num++; 
    $day_count++;
        if ($day_count > 7)
        {
        echo "</tr><tr>";
        $day_count = 1;
        }

} 

while ( $day_count >1 && $day_count <=7 ) 
{ 
    echo "<td> </td>"; 
    $day_count++; 
} 

echo "</tr></table>"; 

1 个答案:

答案 0 :(得分:0)

您需要将数据库代码放到输出日期单元格的位置。这是代码:

<?php
include ("connection.php"); 

$date =time (); 
$day = date('d', $date); 
$month = date('m', $date); 
$month = $month + "01";
$year = date('Y', $date); 

$first_day = mktime(0,0,0,$month, 1, $year); 
$title = date('F', $first_day); 
$day_of_week = date('D', $first_day); 
switch($day_of_week){ 
    case "Sun": $blank = 0; break; 
    case "Mon": $blank = 1; break; 
    case "Tue": $blank = 2; break; 
    case "Wed": $blank = 3; break; 
    case "Thu": $blank = 4; break; 
    case "Fri": $blank = 5; break; 
    case "Sat": $blank = 6; break; 
}

$days_in_month = cal_days_in_month(0, $month, $year);
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
$day_count = 1;
echo "<tr>";

while ( $blank > 0 ) 
{ 
    echo "<td></td>"; 
    $blank = $blank-1; 
    $day_count++;
} 
$day_num = 1;
 while ( $day_num <= $days_in_month ) 
{ 
    //database code here
    $result = ''; //formatted html result
    echo "<td> $day_num $result</td>"; 
    $day_num++; 
    $day_count++;
    if ($day_count > 7)
    {
        echo "</tr><tr>";
        $day_count = 1;
    }
} 

while ( $day_count >1 && $day_count <=7 ) 
{ 
    echo "<td> </td>"; 
    $day_count++; 
} 

echo "</tr></table>";