I'm trying to learn PHP with the newest CodeIgniter framework, but I'm having some problems. I can't tell if I'm just having some bad luck or if I'm missing some fundamental concepts.
Here's my View/index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<?php foreach($forums as $f): ?>
<table>
<tr><td style="background-color:#ccc; font-weight:bold border: 1px solid black;">
<?php echo $f['name']; ?>
</td><开发者_开发技巧;/tr></table><br />
<?php endforeach; ?>
</body>
</html>
And here's my Models/index.php :
class Index_Model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getIndexListing(){
$query = $this->db->query('Select name from Forums where parentid=0 order by sortorder asc');
$rows = $query->result_array();
$query->free_result();
}
}
And here's my Controllers/index.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Index extends CI_Controller {
function __construct()
{
parent::__construct();
// load users model
if (! isset($this->Index_Model))
{
// $this->load->model( 'Index_Model' );
}
}
function index()
{
$data['forums']= $this->Index_Model->getIndexListing();
$data['title'] = 'Welcome!';
$this->load->view('index.php', $data);
}
}
Problem : I can't figure out how to call getIndexListing()
. When I do as I have here, I get the error Undefined property: Index::$Index_Model
. But when I uncomment the $this->load->model( 'Index_Model' );
, I get out of memory exceptions.
What's the right way to call getIndexListing()
so I can populate my page? Did I misname my classes or files?
first of all you need to change controller name as index is the reserved name http://codeigniter.com/user_guide/general/reserved_names.html
Then you will have to have two different names for controller class and model class they cant be same as you cant have 2 classes with same name in same namespace. So let say you controller is whatever then it is good practice to have model class like whatever_model
精彩评论