用PHP foreach代码填充日历 [英] Populating a calendar with PHP foreach code

查看:59
本文介绍了用PHP foreach代码填充日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下jQuery日历: https://github.com/MrHus /jquery-monthly-ical/tree/

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."}    
            ] 

但是,我想用一个foreach语句用PHP变量填充以上数组.我已经知道了,但是它不起作用,没有错误,没有警告,但是没有显示该事件:

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
}
?>

该代码位于.tpl文件中,该文件通过.class.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.

推荐答案

这应该做到:

$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 ?>

这篇关于用PHP foreach代码填充日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆