calendarxp Site Admin
Joined: 30 Jan 2005 Posts: 409
|
Posted: Wed Jan 25, 2006 2:51 am Post subject: How to dynamically generate the agenda/events using PHP and MySQL? |
|
|
1st, create a database table in MySQL with the following scripts:
Code: | CREATE TABLE `my_agenda` (
`ag_year` smallint NOT NULL,
`ag_month` smallint NOT NULL,
`ag_day` smallint NOT NULL,
`ag_message` varchar(255) default '',
`ag_action` varchar(255) default '',
`ag_bgcolor` varchar(20) default 'null',
`ag_fgcolor` varchar(20) default 'null',
`ag_bgimg` varchar(255) default 'null',
`ag_boxit` varchar(20) default 'null',
`ag_html` varchar(255) default 'null',
PRIMARY KEY (`ag_year`,`ag_month`,`ag_day`)
) |
2nd, create a file named agenda.php with the following contents:
Code: | <?
header("Content-Type: text/javascript");
$lnk=mysql_connect("<db_hostname>", "<db_user>", "<db_password>");
mysql_select_db("<db_name>",$lnk) or die( "Unable to select database");
$query="SELECT * FROM my_agenda";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
for ($i=0; $i < $num; $i++) {
$year=mysql_result($result,$i,"ag_year");
$month=mysql_result($result,$i,"ag_month");
$day=mysql_result($result,$i,"ag_day");
$msg=mysql_result($result,$i,"ag_message");
$action=mysql_result($result,$i,"ag_action");
$bgcolor=mysql_result($result,$i,"ag_bgcolor");
$fgcolor=mysql_result($result,$i,"ag_fgcolor");
$bgimg=mysql_result($result,$i,"ag_bgimg");
$boxit=mysql_result($result,$i,"ag_boxit");
$html=mysql_result($result,$i,"ag_html");
?>
fAddEvent(<?=$year?>,<?=$month?>,<?=$day?>,"<?=$msg?>","<?=$action?>",
"<?=$bgcolor?>","<?=$fgcolor?>","<?=$bgimg?>","<?=$boxit?>","<?=$html?>");
<?
}
?>
// -- you may append additional javascript here, e.g. fHoliday() --
| Please remember to use the real db name, user id and password to replace the placeholders in above code.
Finally, upload the agenda.php to your web server (suppose it's placed at /php-cgi/agenda.php) and replace the agenda.js in the name and id of the <iframe> calendar tag with the full path to agenda.php:
Code: | <iframe name="gToday:normal:/php-cgi/agenda.php" name="gToday:normal:/php-cgi/agenda.php" ...> |
Now put in some records in the db table and load up your page - you should see the agenda events showing up nicely in the calendar.
Note that you can still append the fHoliday() function, as in agenda.js, to the end of the agenda.php so as to retain the holidays. And in the agenda tutorial you may also find an example written in JSP code.
Last but not least, please be careful if you would like to use double-quote char (") in the strings - you should escape them wisely so as not to mess up the string boundary of javascript. If you were encountering any error, please point your browser directly to the URL of agenda.php, then either "view page source" or view the downloaded file - it should reveal the generated content. Make sure the content is pure valid javascript (no <script> tags allowed), as if it were in an agenda.js file.
_________________ Copyright 2003-2011 Idemfactor Solutions, Inc. All rights reserved. |
|