简体   繁体   English

在CodeIgniter中显示数据库表

[英]Display database table in CodeIgniter

I am trying to display a table using CodeIgniter . 我试图使用CodeIgniter显示一个表。 I made a function to select all data from one table and display it using a foreach loop when the button is clicked. 我做了一个函数来从一个表中选择所有数据,并在单击按钮时使用foreach循环显示它。 I am getting this error: 我收到此错误:

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

This is my controller page: 这是我的控制器页面:

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

This is the model: 这是模型:

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

This is the view: 这是观点:

<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>

Just change your model method code to 只需将模型方法代码更改为

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

Hope this helps. 希望这可以帮助。 Thanks!! 谢谢!!

you have to use get() 你必须使用get()

select() query builder is used for selecting the columns of the table and not the table select()查询构建器用于选择表的列而不是表

example

$query = $this->db->select(array('product_id','auction_id'))
                  ->get('products');
return $query->result();

if you want to select all you can use the get only, 如果你想选择所有你可以使用get only,

read more at http://ellislab.com/codeigniter/user-guide/database/active_record.html#select 更多信息,请访问http://ellislab.com/codeigniter/user-guide/database/active_record.html#select

Your problem is here: 你的问题在这里:

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

$query->result() is returning false probably because the products table does not exist. $query->result()返回false可能是因为products表不存在。 you have to use get instead of select. 你必须使用get而不是select。

Try: 尝试:

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

That could get your started 这可以让你开始

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

    return $query;
}

I hope the above code will help you. 我希望上面的代码可以帮到你。

There is actually a simpler way available. 实际上有一种更简单的方法。

You should get most from the framework features it is providing, 你应该从它提供的框架功能中获得最大的收益,

Use, CodeIgniter's Table Library, 使用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

You can also modify the behavior of HTML generator if you want some custom things like class in table head or body or anything, which you will almost need. 如果你想要一些自定义的东西,例如桌面或正文中的类或任何你需要的东西,你也可以修改HTML生成器的行为。

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

Use this line after loading the table library. 加载表库后使用此行。 Use keys from the documentation link below. 使用下面文档链接中的键。

Reference: CodeIgniter 3 Table Library - Official Docs 参考: CodeIgniter 3表库 - 官方文档

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

Above code will help you. 以上代码将帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM