我目前正在制作一个活动日历php项目。我从Github - Calendrio获得了这个开源日历脚本,它允许向日历添加事件,但它从“data.js”中的变量“codropsEvents”调用其事件数据。现在我希望从mysql数据库中动态调用事件。
调用日历脚本的功能(注意Caldata:codropsEvents)
<script type="text/javascript">
$(function() {
var transEndEventNames = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'oTransitionEnd',
'msTransition' : 'MSTransitionEnd',
'transition' : 'transitionend'
},
transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
$wrapper = $( '#custom-inner' ),
$calendar = $( '#calendar' ),
cal = $calendar.calendario( {
onDayClick : function( $el, $contentEl, dateProperties ) {
if( $contentEl.length > 0 ) {
showEvents( $contentEl, dateProperties );
}
},
caldata : **codropsEvents**,
displayWeekAbbr : true
} ),
$month = $( '#custom-month' ).html( cal.getMonthName() ),
$year = $( '#custom-year' ).html( cal.getYear() );
$( '#custom-next' ).on( 'click', function() {
cal.gotoNextMonth( updateMonthYear );
} );
$( '#custom-prev' ).on( 'click', function() {
cal.gotoPreviousMonth( updateMonthYear );
} );
function updateMonthYear() {
$month.html( cal.getMonthName() );
$year.html( cal.getYear() );
}
// just an example..
function showEvents( $contentEl, dateProperties ) {
hideEvents();
var $events = $( '<div id="custom-content-reveal" class="custom-content-reveal"><h4>Events for ' + dateProperties.monthname + ' ' + dateProperties.day + ', ' + dateProperties.year + '</h4></div>' ),
$close = $( '<span class="custom-content-close"></span>' ).on( 'click', hideEvents );
$events.append( $contentEl.html() , $close ).insertAfter( $wrapper );
setTimeout( function() {
$events.css( 'top', '0%' );
}, 25 );
}
function hideEvents() {
var $events = $( '#custom-content-reveal' );
if( $events.length > 0 ) {
$events.css( 'top', '100%' );
Modernizr.csstransitions ? $events.on( transEndEventName, function() { $( this ).remove(); } ) : $events.remove();
}
}
});
</script>
我用我在页面头部包含的php函数'getEvents'更改了'codropsEvents'。下面是替换的行和我的php函数,但日历没有显示
caldata: { <?= getEvents(); ?> },
// The getEvents() function
$query_rsgetevents = "SELECT * FROM calendar_events";
$rsgetevents = mysql_query($query_rsgetevents, $rsCalData) or die(mysql_error());
$row_rsgetevents = mysql_fetch_assoc($rsgetevents);
$totalRows_rsgetevents = mysql_num_rows($rsgetevents);
function getEvents() {
$i=0;
while ($i< $totalRows_rsgetevents) {
$month=mysql_result($rsgetevents,$i,"event_month");
if(strlen($month) < 2) { // this is incase only 1 digit was enter e.g. month 5 instead of 05
$month = "0" . $month;
}
$day=mysql_result($rsgetevents,$i,"event_day");
if(strlen($day) < 2) {
$day = "0" . $day;
}
$year=mysql_result($rsgetevents,$i,"event_year");
$title=mysql_result($rsgetevents,$i,"event_title");
if ($i < $totalRows_rsgetevents -1) {
echo "'$month-$day-$year' : '$title'";
} else {
echo "'$month-$day-$year' : '$title'";
}
$i++;
}
}
或者有更好的方法吗?