CodeIgniter Forums

Full Version: list data from two table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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 />';