Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

Javascript with PHP Foreach

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>

I want to generate the events with a foreach because the events are all stored in a database. I tried to do the following:

events: { <?php foreach($events as $event): ?> "<?php echo $event->date; ?>"...

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.

UPDATE

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) "State Large Group" ["school"]=> string(9) "NHS Bands" ["date"]=> string(9) "4-16-2014" ["showHome"]=> string(1) "1" }...

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": {}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
events: <?php echo json_encode($events) ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...