douzhi1924 2016-11-05 06:52
浏览 59
已采纳

如何在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.

  • 写回答

2条回答 默认 最新

  • doupeng5320 2016-11-05 07:01
    关注

    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);
        }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug