CodeIgniter Forums
Fetching db details displaying in a table dynamicalls - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Fetching db details displaying in a table dynamicalls (/thread-33443.html)



Fetching db details displaying in a table dynamicalls - El Forum - 08-25-2010

[eluser]oldrock[/eluser]
Hi..,

I m new to code igniter , the issue is i m unable to fetch the multiples rows from a database using model and diplay the data in the view in a table structure dynamically could anybody plz help me with the sample code.

thanks in advance


Fetching db details displaying in a table dynamicalls - El Forum - 08-26-2010

[eluser]WanWizard[/eluser]
Controller:
Code:
class Test Extends Controller
{
    function Test()
    {
        parent::Controller();
    }

    function index()
    {
        // data array for the view
        $data = array();

        // load the model
        $this->load->model('my_model');

        // fetch the rows
        $data['rows'] = $this->my_model->get_rows();

        //show the view
        $this->load->view('my_view', $data);
    }
}

Model:
Code:
class My_model Extends Model
{
    function My_model()
    {
        parent::Model();
    }

    function get_rows()
    {
        // fetch the rows
        $query = $this->db->get('table');

        // return the result
        if ( $query )
        {
            return $query->result();
        }
        else
        {
            return FALSE;
        }
    }
}

View:
Code:
<?php if ( $rows ) { ?>
    <table>
    &lt;?php foreach ($rows as $row) { ?&gt;
        <tr>
            <td>&lt;?=$row->field ?&gt;</td>
        </tr>
    &lt;?php } ?&gt;
    </table>
&lt;?php } ?&gt;