2013-02-25 187 views
4

我正在嘗試使用CodeIgniter來顯示錶。我做了一個函數來選擇一個表中的所有數據,並在點擊按鈕時使用foreach循環顯示它。我收到此錯誤:在CodeIgniter中顯示數據庫表

Fatal error: Call to undefined method CI_DB_mysql_driver::result() in C:\Xampp\htdocs\Auction\application\models\bidding_model.php on line 47 

這是我的控制器頁:

public function viewauction() 
{ 
    $this->load->model('bidding_model'); 
    $data['query'] = $this->bidding_model->viewauction(); 
    $this->load->view('auction_view', $data); 
} 

這是在型號:

function viewauction() 
{ 
    $query = $this->db->select('products'); 
    return $query->result(); 
} 

這是視圖:

<tbody> 
<?php foreach($query as $row): ?> 
<tr> 
    <td><?php echo $row->product_id; ?></td> 
    <td><?php echo $row->auction_id; ?></td> 
    <td><?php echo $row->start_time; ?></td> 
    <td><?php echo $row->end_time; ?></td> 
</tr> 
<?php endforeach; ?> 
</tbody> 

回答

3

只要將您的模型方法代碼更改爲

function viewauction() 
{ 
    $query = $this->db->select('*')->from('products')->get(); 
    return $query->result(); 
} 

希望這會有所幫助。謝謝!!

0

你的問題是在這裏:

$query = $this->db->select('products'); 
return $query->result() ; 

$query->result()是返回false可能是因爲產品表中不存在。你必須使用get而不是select。

嘗試:

$query = $this->db->get('products'); 
return $query->result() ; 

,可以讓你開始

0
public function select($table, $field, $value) 
{ 
    $this->db->select(*); 
    $this->db->from('$table'); 
    $this->db->where($field, $value); 
    $query = $this->db->get(); 

    return $query; 
} 

我希望上面的代碼會幫助你。

0

實際上有一種更簡單的方法可用。

你應該從框架最多的功能是提供,

使用,CodeIgniter的表庫,

$this->load->library('table'); // Loading the Table Library 

$query = $this->db->get('table_name'); // the MySQL table name to generate HTML table 

echo $this->table->generate($query); // Render of your HTML table 

您還可以,如果你想要一個像類的一些定製的東西修改HTML生成的行爲在桌子的頭部或身體或任何東西,你幾乎需要。

$this->table->set_template($template); // passing an array 

加載表庫後使用此行。使用下面文檔鏈接中的鍵。

參考:CodeIgniter 3 Table Library - Official Docs

0
function viewauction() 
{ 
    $this->db->select('*'); 
    $this->db->from('tablename'); 
    $query = $this->db->get(); 
    return $query->result(); 
} 

上面的代碼將幫助你。

相關問題