开发者

turn array results of CodeIgniter in model to a table

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_ro开发者_Python百科ws() > 0 )
    {
        $row = $query->row_array();


        return $row;
    }

}


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜