Reputation: 10747
I've just started learning Code Igniter and I've run in to a problem almost straight away.
Using the code below and a database setup with the correct privileges I get a 500 error (server error). CI is not throwing an error and I can't see anything wrong with this code.
I'm using the latest version of CI and testing this code:
Controller: products.php
class Products extends CI_Controller {
public function index() {
$this->load->model('products');
$this->load->view('products');
}
}
Model: products.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
I've put these classes in the directories they should be in with a filename of the same name as the class....if I comment out the load-model code in the controller my view is shown. My database connections are correct because if I change them to something incorrect CI throws an error. There's some sort of problem with the model or the loading of it.
Upvotes: 0
Views: 1354
Reputation: 3369
A good convention to use is to name your controller ProductsController and then leave the model Products. So all controllers are SomethingController.
Example:
class ProductsController extends CI_Controller
where the filename become products_controller.php
and the model remains:
class Products extends CI_model
and remains products.php
Now your code should work.
Upvotes: 0
Reputation: 2992
Try this:
class Products extends CI_Controller {
public function index() {
$this->load->model('products_model','products');
$this->load->view('products');
}
}
Model: products_model.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
Upvotes: 1
Reputation: 20753
You have conflicting class names, the modell and the controller class both called Products
.
Upvotes: 2