Richard Doe
Richard Doe

Reputation: 79

How to Loop Data in Codeigniter's HTML Table Class?

I need to properly loop Customer's name in Codeigniter's HTML table library, but I don't how to properly use it.

So, here is my model:

<?php class Dash_model extends CI_Model {

        public function __construct()
        {
                parent::__construct();
                $this->load->database();
        }

        public function customerlist()
        {
            $query = $this->db->query("SELECT customername FROM customertable ORDER BY customerid");

            foreach ($query->result() as $row)
            {
                echo $row->customers;
            }

        }

And then here's my Controller:

class Dash_control extends CI_Controller {

    public function __construct()
       {
            parent::__construct();
            $this->load->model('dash_model');
            $this->load->library('table');
       }

    public function index()
    {
        $tmpl = array (
        'row_start'           => '<tr>',
        'row_end'             => '</tr>',
        'cell_start'          => '<td>',
        'cell_end'            => '</td>',
        );

        $this->table->set_template($tmpl); 

        $data['customerloop'] = $this->dash_model->customerlist();

        $this->load->view('dashboard',$data);
    }

After this the code was working perfectly, I can loop the customers names but I don't know how to pass them into my view file. I'm using bootstrap table, so I thought I can make it work with this:

<tbody>
<td><?php echo $customerloop;?></td>
</tbody>

but of course it didn't work in a table, it's just looping like that without a table.

One more question, does Codeigniter's table works in Bootstrap? Or it's already responsive without the bootstrap? I'm pretty new to Codeigniter so thank you for every advice you can give me guys...

Upvotes: 1

Views: 2394

Answers (2)

Sharmistha Das
Sharmistha Das

Reputation: 175

Model:

    public function __construct()
    {
            parent::__construct();
            $this->load->database();
    }

    public function customerlist()
    {
        $query = $this->db->query("SELECT customername FROM customertable ORDER BY customerid");

        return $query->result(); 


    }

Controller:

class Dash_control extends CI_Controller {

public function __construct()
   {
        parent::__construct();
        $this->load->model('dash_model');
        $this->load->library('table');
   }

public function index()
{
    $tmpl = array (
    'row_start'           => '<tr>',
    'row_end'             => '</tr>',
    'cell_start'          => '<td>',
    'cell_end'            => '</td>',
    );

    $this->table->set_template($tmpl); 

    $data['customerloop'] = $this->dash_model->customerlist();

    $this->load->view('dashboard',$data);
}

View:

<tbody>
<?php foreach ($customerloop as $row)
 { ?>
<tr><td><?php echo $row->customers; ?></td></tr>
<?php } ?>
</tbody>

Upvotes: 0

Angel
Angel

Reputation: 612

You can use html table class in view page

 <table border="1" class="table table-bordered table-hover dataTable">
  <tr role="row">
  <th class="sorting" width="5%">Customer data</th>                                         
     <?php
        foreach ($customerloop as $row) {
      ?><tr>  
      <td class=" "><?php echo $row->data; ?></td>  

  </tr>  
  <?php }
     ?>  
     </table>

Upvotes: 1

Related Questions