⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

some help with n to n relationship (CodeIgniter)



ceroberoz

ceroberoz
  • profile picture
  • Member

Posted 08 January 2014 - 21:18 PM

let say I have this table.

relation-n-n.png

 

and the following code

Model

function get_film_info($film_id){
	$this->db->select('*')
			 ->from('film')
			 ->join('film_actor','film_actor.film_id = film.film_id', 'left')
			 ->join('actor','actor.actor_id = film_actor.actor_id','left')
			 ->where('film',$film_id);

	$query = $this->db->get();

	if($query->num_rows() > 0){
	   return $query->result();
	}
	else{
	  return array();
	}
}

Controller

function film(){
	$this->load->model('film');
	$this->load->helper(array('text','url'));
	
	$id = $this->uri->segment(3);
	$data['data'] = $this->film->get_film_info($id);
	$this->load->view('welcome_message',$data);
}

View

<?php foreach ($data as $row): ?>
	<?php echo $row->fullname;?>
<?php endforeach;?>

the table film has one value, FilmA

the table actor has two value, Actor1 & Actor1

 

both actor already on FilmA

the result is Actor1 Actor2.

 

my question is that possible to separate the result as dropdown?

 

the nearest hint I get is this one but it doesn't use n to n relation.

Model

function get_all() {
  $results = $this->db->get('produk')->result();
  foreach ($results as &$result) {
   if ($result->nilai_pilihan) {
    $result->nilai_pilihan = explode(',',$result->nilai_pilihan);
   } 
  }
  return $results;
 }

the nilai_pilihan use varchar "option 1" & "option2" with "," as separator.

 

again, is that possible to separate the result as dropdown?

 

any hint is welcome, thanks before.