Welcome Guest, Not a member yet? Register   Sign In
list data from two table

If I have in one table data in columns: id_first, name. Example:

1, Joe
2, MIchael
3, Kim

And in second have data in columns id_second, id_first, lastname. Example"

1, 3, Basinger
2, 3, Basinger
3, 1, Jordan
4. 2, Arena

To get table in view likse this:

1, Kim, Basinger
2, Kim, Basinger
3, Joe, Jordan
4, Michael, Arena

How to write all Model, View and Controller?

(This post was last modified: 03-13-2016, 12:23 PM by Wouter60.)

In your model, use the query builder with a join statement:
PHP Code:
function get_fullnames()
 ->from('fullnames t1')
 ->join('firstnames t2','t1.id_first = t2.id_first');
 $query $this->db->get();
 return $query->result();  //returns an array of objects

In your controller:
PHP Code:
$data['records'] = $this->names_model->get_fullnames();  //assuming that your model is called 'names_model'
$this->load->view('shownames',$data);  //assuming that your view is called 'shownames' 

In your view:
PHP Code:
foreach ($records as $record) {
 echo  $record->id_second ', ' $record->name ', ' $record->last_name '<br />';


Theme © iAndrew 2016 - Forum software by © MyBB