2012-03-08 218 views
0

我正在按照教程,並得到了500問題。我不確定問題是否由模型引起。 我的模型:500內部服務器錯誤CodeIgniter

<?php 

class Cat_model extends CI_Model{ 


public function __construct() 
{ 
    $this->load->database(); 
} 

function getCategory($id){ 
    $data = array(); 
    //select one row matching that ID from the categories table 
    $options = array('id'=>$id); 
    $q = $this->db->get_where('categories',$options,1); 

    if($q->num_rows()>0){ 
     $data = $q->row_array(); 
    } 

    $q->free_result(); 
    return $data; 
} 

function getAllCategories(){ 
    $data = array(); 
    $q = $this->db->get('categories'); 

    if($q->num_rows()>0){ 
     foreach ($q->result_array() as $row){ 
      $data[] = $row; 
     } 
    } 
    $q->free_result(); 
    return $data; 
} 
} 

我的控制器:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Welcome extends CI_Controller { 

    public function __construct() 
{ 
    parent::__construct(); 
    } 


public function index() 
{//homepage 
     $data['title'] = "Welcome to TM testDIY"; 
     $data['navlist'] = $this->cat_model->getAllCategories(); 
     $this->load->var($data); 
     $this->load->view('template'); 

} 

} 

數據庫

$db['default']['database'] = 'testDIY'; 

,我已經在自動加載自動加載文件中的模型。

如果我清空數據庫或將一些隨機名稱放在那裏,它不會顯示500內部服務器錯誤,而是顯示一些有意義的錯誤消息。 現在我無法弄清楚這個問題,誰都可以幫忙?

+0

這意味着一般的服務器錯誤。也許這是一個不好的要求。 – gooddadmike 2012-03-08 19:44:54

回答

1

問題似乎是在這裏你已經使用
$this->load->var($data);
而不是
$this->load->vars($data);

+0

嘿,山陰,謝謝!它現在有效! – Mario 2012-03-09 11:58:29

0

你沒有加載模型控制器

public function index() 
{//homepage 

//Load the model 
$this->load->model('cat_model','',TRUE); 

    $data['title'] = "Welcome to TM testDIY"; 
    $data['navlist'] = $this->cat_model->getAllCategories(); 
    $this->load->var($data); 
    $this->load->view('template'); 

} 

你還需要在$數據發送到您的視圖

$這個 - >負載>視圖(「模板」,$數據);

+0

仔細閱讀問題,他提到他的模型是自動加載的。 – 2012-03-09 07:42:47

+0

我想念對不起 – 2012-03-09 14:00:33