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

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


Code:
1, Joe
2, MIchael
3, Kim


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


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


To get table in view likse this:


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


How to write all Model, View and Controller?
Reply
#2

(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()
{
 
 $this->db
  
->select('t1.*,t2.name')
 
 ->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 />';

Reply




Theme © iAndrew 2016 - Forum software by © MyBB