CodeIgniter在视图中抱怨未定义的变量,print_r显示结果

时间:2013-06-14 18:11:13

标签: php codeigniter

我在基本的PHP codeigniter应用程序中看到了一些奇怪的行为。

我的输出如下:

number of rows:  

    Warning notice  
    undefined variable: rows

number of rows: 10

最奇怪的是,它似乎试图加倍执行我的PHP代码,但我无法弄清楚原因。任何见解都表示赞赏:

以下代码:

模特:

class foo extends CI_Controller  
{  
    public function index()  
    {  
           $this->go();
    }    

     public function go()  
     {  
        $this->load->model('model');
        $data = array('rows'=>  $this->model->count());  
        $this->load->view('view',$data);
     }  
}  

模特:

class model extends CI_Model  
{  
      public function count()  
      {  
          $query = "Select count(1) as count from table";
          $result = $this->db->query($query);    
          return $result->result_array();
      }   
}  

观点:

<html>  
<body> 
     Number of rows:  <?php print_r($rows[0]['COUNT']); ?>  
</body>
</html>  

1 个答案:

答案 0 :(得分:2)

看起来你的索引函数正在触发并在调用go之前调用go()。

我猜你的htaccess规则写得不正确。

尝试将索引功能更改为此

public function index()  
{  
    redirect('index.php/foo/go');
}