【问题标题】:Populating a calendar with PHP foreach code使用 PHP foreach 代码填充日历
【发布时间】:2013-01-25 18:24:17
【问题描述】:

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

按照文档,它告诉我应该像这样输入日期:

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 变量填充上述数组。我有这个,但是它不起作用,没有错误,没有警告,但是没有显示该事件:

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

这是之前在日历之外的代码,在我的表格中,它可以工作:

<?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 文件显示。在其中,是运行必要变量的所有查询。

【问题讨论】:

    标签: php jquery mysql jquery-plugins


    【解决方案1】:

    应该这样做:

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

    【讨论】:

    • 它正在显示日历,但它仍然不显示事件。
    • pastebin.com/Qc8GByBE。我首先尝试了您的建议:'date' =&gt; date('Y/m/d') 然后将其更改回我的版本,看看它是否有效。
    • 它有效,但出于某种奇怪的原因,标题显示为 null,并且事件显示在今天的日期。有什么想法吗?
    • 我错过了$event,我的错误,但它仍然显示在今天的日期......
    • 因为两者具有相同的索引并被+ 覆盖。但这不应该发生在 array_merge 中。试试array_merge(array_valyes($history), array_values($events))
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2017-10-01
    相关资源
    最近更新 更多