2013-02-25 122 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(); 
} 

上面的代码将帮助你。

相关问题