zzwyb89
zzwyb89

Reputation: 470

Populating a calendar with PHP foreach code

I am using the following jQuery calendar: https://github.com/MrHus/jquery-monthly-ical/tree/

Following the documentation, it tells me that dates should be entered like this:

eventdates: [{"date": "2009-03-21", "title": "My birthday", "desc": "Its my birthday!"},
             {"date": "yyyy-01-01", "title": "New Year", "desc": "Its a new year!"},
             {"date": "2009-mm-01", "title": "New Month", "desc": "First day of the new month!"},
             {"date": "2010-mm-01", "title": "New Month", "desc": "First day of the new month!"},
             {"date": "2010-09-01", "title": "Convention", "desc": "September convention."}, 
             {"date": "2010-09-02", "title": "Convention", "desc": "September convention day two."}, 
             {"date": "2010-mm-01", "title": "Towl", "desc": "Dont forget to bring a towl."}    
            ] 

However, I'd like to populate the above arrays with PHP variables, with a foreach statement. I've got this, however it doesn't work, no errors, no warnings, but the event is not displayed:

        <?php
        foreach($events as $event)
        {
        ?>
            eventdates: [{"date": "<?php date('Y/m/d') ?>", "title": "<?php $event->title ?>", "desc": "<a href="<?php  echo SITE_URL ?>/index.php/events/get_event?id=<?php $event->id ?>">Details/Signups</a>"},]
        <?php
        }
        ?>

This is the previous code outside of calendar, and inside a table I had, and it works:

<?php
if(!$events)
{
    echo 'No Upcoming Events';
}
else
{
    ?>
<center>
    <table border="1px" width="80%">
        <tr>
            <td width="25%"><b>Date:</b></td>
            <td width="60%"><b>Event:</b></td>
            <td><b>Details/Signups</b></td>
        </tr>
            <?php
            foreach($events as $event)
            {
                if($event->active == '2')
                {
                    continue;
                }
        echo '<tr><td>'.date('n/j/Y', strtotime($event->date)).'</td>';
        echo '<td>'.$event->title.'</td>';
        echo '<td><a href="'.SITE_URL.'/index.php/events/get_event?id='.$event->id.'">Details/Signups</a></td></tr>';
    }
    ?>
    </table>
</center>
    <?php
}
?>

The code is inside a .tpl file, which is shown via .class.php file. Inside of it, are all of the queries to run necessary variables.

Upvotes: 0

Views: 1418

Answers (1)

nice ass
nice ass

Reputation: 16719

This should do it:

$dates = array();

// build an array with that data
foreach($events as $event)
  $dates[] = (object)array(
     'date'  => date('Y/m/d'),
     'title' => $event->title,
     'desc'  => sprintf('<a href="%s/index.php/events/get_event?id=%s">Details/Signups</a>', SITE_URL, $event->id),
  );

?>

eventdates:

<?php print json_encode($dates); // print it as a json string ?>

Upvotes: 1

Related Questions