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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?