I want to join table dynamically in codeigniter by passing table name as parameter. below is my controller function to call model function
function index(){
$table=array('branch'=>'branch_id','specialization'=>'spec_branch_id');
$this->model->join($table);
}
and this is my model function
function join($table){
foreach($table as $table_name=>$table_id){
/*i want here table*/
$table1=$table_name;
}
$this->db->select('*');
$this->db->from(''.$table1.' t1');
$this->db->join(''.$table2.' t2','t1.'.$t1id.'=t2.'.$t2id);
return $this->db->get();
echo $this->db->last_query();die;
}
As in above function i want dynamic table name like table1=branch; table2=specializaton in my model function, so please help me to solve and if someone have another also can share.
here is my custom code dynamically join.
mode form.
<?php public function commonGet($options) {
$select = false;
$table = false;
$join = false;
$order = false;
$limit = false;
$offset = false;
$where = false;
$or_where = false;
$single = false;
$where_not_in = false;
$like = false;
extract($options);
if ($select != false)
$this->db->select($select);
if ($table != false)
$this->db->from($table);
if ($where != false)
$this->db->where($where);
if ($where_not_in != false) {
foreach ($where_not_in as $key => $value) {
if (count($value) > 0)
$this->db->where_not_in($key, $value);
}
}
if ($like != false) {
$this->db->like($like);
}
if ($or_where != false)
$this->db->or_where($or_where);
if ($limit != false) {
if (!is_array($limit)) {
$this->db->limit($limit);
} else {
foreach ($limit as $limitval => $offset) {
$this->db->limit($limitval, $offset);
}
}
}
if ($order != false) {
foreach ($order as $key => $value) {
if (is_array($value)) {
foreach ($order as $orderby => $orderval) {
$this->db->order_by($orderby, $orderval);
}
} else {
$this->db->order_by($key, $value);
}
}
}
if ($join != false) {
foreach ($join as $key => $value) {
if (is_array($value)) {
if (count($value) == 3) {
$this->db->join($value[0], $value[1], $value[2]);
} else {
foreach ($value as $key1 => $value1) {
$this->db->join($key1, $value1);
}
}
} else {
$this->db->join($key, $value);
}
}
}
$query = $this->db->get();
if ($single) {
return $query->row();
}
return $query->result();
} ?>
and view index page
<?php function index(){
$option = array(
'table' => 't1',
'join' => array('t2' => 't2.id = t1.id')
);
$this->model->commonGet($option);
}?>
You can try this code also for two table join.
Controller function.
function index(){
$table=array('branch'=>'branch_id','specialization'=>'spec_branch_id');
$this->model->join($table);
}
Model function
function join($tables){
$i=1;
foreach($tables as $table_name=>$table_id){
${'table'.$i}=$table_name;
${'t'.$i.'id'}=$table_id;
$i++;
}
$this->db->select('*');
$this->db->from(''.$table1.' t1');
$this->db->join(''.$table2.' t2','t1.'.$t1id.'=t2.'.$t2id);
//$this->output->enable_profiler(TRUE);
return $this->db->get();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.