0

I have the following code in my model but want the results to be returned in a table rather than array format. How do I do that?

function info_car ( $id )
{

    $this->db->select( 'car_id,car_vin_number,car_make,car_model,car_year,car_color,location_location_id,source_source_id' );
    $this->db->where( 'car_id', $id );
    $this->db->from('car');

    $query = $this->db->get();

    if ( $query->num_rows() > 0 )
    {
        $row = $query->row_array();


        return $row;
    }

}

3 Answers 3

2

use the HTML table class, a (slightly inflexible) solution, but it does the job very nicely and quickly

$this->load->library('table');

$query = $this->db->query("SELECT * FROM my_table");

echo $this->table->generate($query);

or just throw it an array of data :

$data = array(
             array('Name', 'Color', 'Size'),
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')   
             );
echo $this->table->generate($data);

read more here : http://codeigniter.com/user_guide/libraries/table.html

Sign up to request clarification or add additional context in comments.

Comments

1

Well, strictly speaking, you shouldn't return a table from your model, just the data that will go into the table.

The controller sends the data to your view which then loops through the data to display a table.

I'll admit it seems convenient to return a table from your model and then pass it along to your view, but if your views have slightly different ways of displaying that data, you'll end up writing lots of similar model functions differing only in how they format the table.

If you return the raw data from your model your views can then format that data according to the needs of the view.

Comments

0

Tables are not native to php. You might be confusing this with HTML tables. If that's the case,

<table>
<?php foreach ($query as $row): ?>
    <tr><?php echo $row->content ?></tr>
<?php endforeach;?>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.