【问题标题】:Notice (8): Undefined offset: 2 [APP/View/Helper/CounterHelper.php注意(8):未定义偏移量:2 [APP/View/Helper/CounterHelper.php
【发布时间】:2018-10-11 18:18:51
【问题描述】:

我是cakephp的新手,我使用的是2.8版本,想在我的主页添加访问计数器,但是我得到以下错误:

Notice (8): Undefined offset: 2 [APP/View/Helper/CounterHelper.php, line 157]
Notice (8): Undefined offset: 2 [APP/View/Helper/CounterHelper.php, line 186]

我把相关的代码结尾给你:

 //Start get detail
        foreach ( $data as $value ){
            $temp = explode ("/", $value['Counter']['date_visit']);
            if ( $temp[0] == $this->Time['mday'] && $temp[1] == $this->Time['mon'] ){
                //Get user online
                if ( $newTime - $value['Counter']['time'] < $this->TimeOut )
                    ++$this->User_Online;
            }

  line 157 ====>  if ( $this->Time['year'] == $temp[2] ){

                //Get today static
                if ( $this_date == $temp[0] )
                    ++$this->Total_Today;

                //Get this month static
                if ( $this_month == $temp['1'] )
                    ++$this->Total_Month;
            }

        }

        //For previous

        $prev_date = $this->Time['mday'] - 1;
        $prev_mon = $this->Time['mon'] - 1;

        if ( $prev_date < 1){
            $prev_date = 30;
            $prev_mon -= 1;
        }

        if ( $prev_mon < 1)
            $prev_mon = 12;

        foreach ($data as $value){
            $temp = explode ("/", $value['Counter']['date_visit']);

  line 186  ====>  if ( $this->Time['year'] == $temp[2] ){
                if ( $prev_date == $temp[0] )
                    ++$this->Total_Yesterday;
                if ( $prev_mon == $temp['1'] )
                    ++$this->Total_Mon_Prev;
            }
        }

提前感谢您的回答。

p.s:涉及的行是:第157行和第186行

【问题讨论】:

  • 这与 CakePHP 无关。您可能想从学习如何在语言文档中查找错误开始。 ;) stackoverflow.com/questions/2508215/undefined-offset-php-error/…
  • 这意味着您的 $temp 数组中没有第二个元素。您可以使用 pr($temp); 来检查 $temp 数组的值;
  • 查看 $data 中的内容很重要
  • 在您的代码剪切以下行之前编写并向我们展示输出是什么:debug($data);

标签: cakephp cakephp-2.x cakephp-2.8


【解决方案1】:

我通过在指示的行之前添加以下代码解决了这个问题

if (is_array($temp))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2023-03-27
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多