var_dump等于null,foreach被破坏

时间:2013-02-19 10:50:25

标签: php javascript mysql foreach

以下代码应生成日历。文档可在此处获取:https://github.com/MrHus/jquery-monthly-ical/tree/。我知道我正在用Javascript做什么,但是我是PHP的初学者,所以如果可能的话我会很感激,很抱歉我的无能,希望你不介意:)这是我的代码:

<script type="text/javascript">
$(document).ready(function()
{      
   $("#ical").ical({
        daynames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        //startOnSunday: true,

        <?php
          $dates = array();
          echo var_dump($events);

          // build an array with that data
          foreach($history as $event)

          $dates[] = (object)array(
                 'date'  => date('Y-m-d', strtotime($event->date)),
                 '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 
                    ?>
            });
        });
</script>

但是,日历不会出现。我添加了一个var_dump,看起来$events返回为空,但我确定不是。

<script type="text/javascript">
    $(document).ready(function()
      {      
        $("#ical").ical
            ({
                daynames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
                //startOnSunday: true,

                NULL

                eventdates:
Uncaught SyntaxError: Unexpected identifier
                        [{"date":"2013-02-18","title":"Maiden Flight","desc":"<a href=\"http:\/\/flyeurova.com\/index.php\/events\/get_event?id=1\">Details\/Signups<\/a>"}]                    });
        });
</script>

我有两个数组,$history$events。我试图在foreach中合并它们,但是所有的尝试都失败了。我只能假设这是因为上述问题。

两个变量都在文件外部设置。首先,我有一个EventsData.class.php文件来执行此操作:

class EventsData extends CodonData
{
    public function get_upcoming_events()
    {
        $query = "SELECT * FROM events
               WHERE date >= NOW()
               ORDER BY date ASC";

        return DB::get_results($query);
    }

    public function get_past_events()
    {
        $query = "SELECT * FROM events
                WHERE date < NOW()
                ORDER BY date DESC";

        return DB::get_results($query);
    }
}

然后Events.php设置变量$history$events,并将它们包含在events_index.tpl文件中,其中插入了上述javascript。

class Events extends CodonModule
{
    public function index()
    {
        $this->set('events', EventsData::get_upcoming_events());
        $this->set('history', EventsData::get_past_events());
        $this->show('events/events_index.tpl');
    }
}

现在,奇怪的是,EXACT相同的代码在我的localhost服务器上运行,但不是在我上传它时。当我尝试在foreach中合并变量时就像我说array_merge(array_values($history), array_values($events))一样 - 它适用于localhost,但Chrome只返回:

<b>Warning</b>:  array_values() [<a href='function.array-values'>function.array-values</a>]: The argument should be an array in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b>
<b>Warning</b>:  array_merge() [<a href='function.array-merge'>function.array-merge</a>]: Argument #2 is not an array in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b>
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b><br />

我真的很困惑,我想让它发挥作用。我在哪里犯了错误,我该如何解决?

0 个答案:

没有答案