Nileshsinh Rathod Nileshsinh Rathod - 25 days ago 12
MySQL Question

How to join table dynamically in codeigniter?

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.

Answer

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);
    }?>
Comments