0
我在我的模型,這個代碼笨:如何在我的視圖中顯示Codeigniter中的adodb數據?
<?php
class User_model extends Model {
function User_model()
{
parent::Model();
}
function get_data()
{
$pages = false;
// Get the pages from the database using adodb if needed
$this->adodb->connect();
$recordSet = $this->adodb->execute('SELECT id, genreID, artist, albumName FROM album');
if (! $recordSet)
{
log_message('error', 'Error connecting to the database');
log_message('debug', $this->adodb->getErrorMsg());
}
else
{
unset($this->pages);
while (!$recordSet->EOF)
{
$pages[] = array(
'id' => $recordSet->fields[0],
'genreID' => $recordSet->fields[1],
'artist' => $recordSet->fields[2],
'albumName' => $recordSet->fields[3]
);
$recordSet->MoveNext();
}
$this->pages = $pages;
}
$this->adodb->disconnect();
}
}
?>
我有這個在我的控制器:
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
//
$this->load->model('User_model');
$data['query'] = $this->User_model->get_data();
$this->load->view('welcome_message',$data);
}
}
什麼我不能做的就是我的模型結果到我的觀點。沒有結果()對象,因爲我用adodb所以:
<?php foreach($query->result() as $row): ?>
<p>
<?=$row->albumName;?>
</p>
<?php endforeach; ?>
給我一個錯誤。
我在視圖中放置了什麼來獲取我的模型的查詢結果。我知道模型的工作原理,因爲我可以echo $ recordSet-> fields [3];從模型中查看專輯名稱。
謝謝你的幫助。
編輯:我不明白爲什麼我的視圖中我的get_data()調用不返回任何內容。
我正在複製這個人的底部http://codeigniter.com/forums/viewthread/65672/我以爲我是從控制器調用查詢的活動記錄模式,但我不知道爲什麼get_data什麼也不返回 – johnny 2010-09-16 21:42:40
我把一個返回$ recordSet,但get_data沒有返回任何視圖。數據僅存在於模型中。 – johnny 2010-09-16 21:54:23
你應該不需要使用$ query-> results(),你是否嘗試過類似於recordSet中模型中完成的迭代?請注意,模型沒有返回值的原因是實現者在模型的屬性($ this-> pages)上設置了值,您可能可以訪問它的$ this-> User_model->頁面,但通常我不添加屬性模型。 – 2010-09-17 14:03:26