nsutgio nsutgio - 15 days ago 6
PHP Question

Selecting records from a database table in php using codeigniter and pass to a view

I'm making a query in php using codeigniter to get the data that I wanted from a database. To be able to get all values for a specific column, I stored it into an array and passed it into a view. But I can do it only for one column.

Here's my code:

$query = $this->db->query('SELECT name,description FROM module');

$result = $query->result_array();

foreach($result as $key => $rowdata){

$resultdata['values'][$key] = $rowdata['name'];

}

$this->load->view('myview',$resultdata);


With this scenario I can get all name from the module table. But my problem is, I also wanted to get all the description in the module table, but I don't know how can I implement it and be able to pass it into the view. Hope someone could help me with this. Thanks!

Answer

hi in you way you will do loop twice in controller and then in view to print it check this out

//in controller

 $query = $this->db->query('SELECT name,description FROM module');

    $resultdata['results'] = $query->result_array();

    $this->load->view('myview',$resultdata);

myview.php

foreach($results as $result)
{
    echo $result['name'],' ',$result['description'];
}