How to create a dynamic calendar with MySQL, PHP etc
I am looking to create a calendar based on some 开发者_如何学Pythonpre-existing event entries. I currently have a event creation system using MySQL and PHP. However, I can only list these in an agenda format, sorted by soonest event.
Is there some existing method of creating a monthly calendar based on events in a database? I have little coding experience, so I don't think I would be able to create my own calendar system, but with some coding help, I might be able to piece something together.
My preferred end result would be to have a calendar with the event titles in the correct day (trimmed down to fit, possibly), with a link to the event page, and on hover, tooltip-like popup (I have something already implemented in the agenda.) Also, I would be willing to use PHP, MySQL, Javascript, or Jquery technologies, as I already have those on my server.
Thanks
This is the smallest, easiest and simplest coding way to generate dynamic calendar in PHP without any jquery. Just copy the below PHP code and save as "index.php" file. And then run this file. A dynamic calendar will be generate with the feature of choose year, month and previous & next month. You can set event titles or link where the dates are displaying. I hope this will help.
<?php
$y = $_GET['y'] ? $_GET['y'] : date('Y');
$m = $_GET['m'] ? $_GET['m'] : date('m');
//display 5 next and 5 previous years of selected year
for ($i=$y-5; $i<=$y+5; $i++){
echo '<a href="index.php?y='.$i.'&m='.$m.'">'.$i.'</a>  ';
}
echo "<br><br>";
//months array just like Jan,Feb,Mar,Apr in short format
$m_array = array('1'=>'Jan', '2'=>'Feb', '3'=>'Mar', '4'=>'Apr', '5'=>'May', '6'=>'Jun', '7'=>'Jul', '8'=>'Aug', '9'=>'Sep', '10'=>'Oct', '11'=>'Nov', '12'=>'Dec');
//display months
foreach ($m_array as $key=>$val){
echo '<a href="index.php?y='.$y.'&m='.$key.'">'.$val.'</a>  ';
}
echo "<br><br>";
$d_array = array('1'=>31, '2'=>28, '3'=>31, '4'=>30, '5'=>31, '6'=>30, '7'=>31, '8'=>31, '9'=>30, '10'=>31, '11'=>30, '12'=>31);
$d_m = ($m==2 && $y%4==0)?29:$d_array[$m];
echo '<table><tr><th colspan="7">'.$m_array[$m].' '.$y.'</th></tr><tr>';
//days array
$days_array = array('1'=>'Mon', '2'=>'Tue', '3'=>'Wed', '4'=>'Thu', '5'=>'Fri', '6'=>'Sat', '7'=>'Sun');
//display days
foreach ($days_array as $key=>$val){
echo '<th>'.$val.'</th>';
}
echo "</tr></tr>";
$date = $y.'-'.$m.'-01';
//find start day of the month
$startday = array_search(date('D',strtotime($date)), $days_array);
//daisplay month dates
for($i=0; $i<($d_m+$startday); $i++){
$day = ($i-$startday+1<=9)?'0'.($i-$startday+1):$i-$startday+1;
echo ($i<$startday)?'<td></td>':'<td>'.$day.'</td>';
echo ($i%7==0)?'</tr><tr>':'';
}
//calculate next & prev month
$next_y=(($m+1)>12)?($y+1):$y;
$next_m=(($m+1)>12)?1:($m+1);
$prev_y=(($m-1)<=0)?($y-1):$y;
$prev_m=(($m-1)<=0)?12:($m-1);
//daisplay next prev
echo '<tr><td><a href="index.php?y='.$prev_y.'&m='.$prev_m.'">Prev</a></td><td></td><td></td><td></td><td></td><td></td><td><a href="index.php?y='.$next_y.'&m='.$next_m.'">Next</a></td></tr>';
?>
You need to break this project down. First, determine your needs and design a database to meet them. For calendars, I prefer to have a table of events, and then a separate table storing the occurrences of those events. This allows for events that repeat. For instance, if I have an event every Monday for the next 10 days, then I will have 10 occurrences in the occurrences table and one event in the events table. Now, the problem with this is that it isn't possible to represent events that repeat infinitely. In my experience, it is better for the user to actually choose an end date, but depending on your application, your users may not like that.
Once you have that database set up, then you can work on the interface.
精彩评论