私は最新のCodeIgniterフレームワークでPHPを学ぼうとしていますが、いくつか問題があります。私はちょうどいくつかの不運を抱いているのか、私はいくつかの基本的な概念が欠けているのかどうかは分かりません。CodeIgniter Modelからどうやって取得するのですか?
は、ここに私のビュー/ 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>
そして、ここでは私のモデル/ 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();
}
}
そして、ここでは
<?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);
}
}
問題のindex.php私のコントローラ/です:getIndexListing()
に電話する方法がわかりません。私がここにいるようにすると、エラーUndefined property: Index::$Index_Model
が出ます。しかし、私が$this->load->model('Index_Model');
のコメントを外すと、メモリの例外が発生します。
getIndexListing()
に電話する正しい方法は何ですか。私は自分のページに入力できますか?私はクラスやファイルを誤って覚えましたか?
あなたのコントローラでは、あなたのモデルの読み込み行をコメントアウトしているかもしれません:$ this-> load-> model( 'Index_Model' );あなたのインデックスコントローラは、エラーが示すように、$ Index_Modelプロパティが定義されていません。 – Endophage
if(!isset($ this-> Index_Model))チェックを削除することができます。 Codeigniterはモデルを一度ロードします。 – Endophage
そして、getIndexListing()関数から値を返さないものを返すことができます。 – Endophage