简体   繁体   中英

Display database table in CodeIgniter

I am trying to display a table using CodeIgniter . I made a function to select all data from one table and display it using a foreach loop when the button is clicked. 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()

select() query builder is used for selecting the columns of the table and not the table

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,

read more at 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. you have to use get instead of 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,

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

$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

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

Above code will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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