带有PHP Foreach的Javascript [英] Javascript with PHP Foreach
问题描述
因此,我正在尝试使用此插件: http://w3widgets.com/sensitive-calendar/
So I am trying to use this plugin: http://w3widgets.com/responsive-calendar/
日历初始化和添加事件的方式如下:
And the way the calendar is initialized and have events added is like so:
<script>
<?php $today = date('Y-m'); ?>
$( document ).ready( function() {
$(".responsive-calendar").responsiveCalendar({
time: '<?php echo $today; ?>',
events: {
"2014-04-30": {"number": 1, "badgeClass": "badge-warning", "url": "http://w3widgets.com/responsive-calendar"},
"2013-04-26": {"number": 1, "badgeClass": "badge-warning", "url": "http://w3widgets.com"},
"2013-05-03": {"number": 1, "badgeClass": "badge-error"},
"2013-06-12": {}}
});
});
</script>
我想用foreach生成事件,因为事件都存储在数据库中.我尝试执行以下操作:
I want to generate the events with a foreach because the events are all stored in a database. I tried to do the following:
事件:{<?php foreach($ events as $ event):?><?php echo $ event-> date;?>" ...
但是我收到一个错误,提示($ events为$ event)
中的)
是意外的.
But I get an error that says the )
in the ($events as $event)
is unexpected.
我该怎么做,我必须能够这样做或日历必须消失.简单明了.
How can I do this, I must be able to or the calendar has to disappear. Plain and simple.
更新
如果我 var_dump()
$ events
,则得到以下字符串(简称为第一个事件!)
If I var_dump()
the $events
I get the following string (shortened to the first event!)
array(3){[0] =>object(stdClass)#21(5){["id"] =>string(1)"1" ["name"] =>string(17)州大团体" [学校"] =>string(9)"NHS Bands" ["date"] =>string(9)"4-16-2014" ["showHome"] =>string(1)"1"} ...
如果我 events:<?php echo json_encode($ events);?>
的编码顺序不正确.它确实:
And If I events: <?php echo json_encode($events); ?>
it doesn't encode in the correct order. It does:
events: [{"id":"1","name":"State Large Group","school":"NHS Bands","date":"4-16-2014","showHome":"1"},{"id":"2","name":"State Solo\/Ensemble","school":"NHS Bands","date":"4-26-2014","showHome":"1"},{"id":"3","name":"League Music Festival","school":"RVMS Bands","date":"4-29-2014","showHome":"1"}] });
当它需要类似以下内容时:
When it needs to be something like:
"2013-06-12": {}
推荐答案
events: <?php echo json_encode($events) ?>
这篇关于带有PHP Foreach的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!