Create dynamic controller and model

- to reduce code , follow below way that reduce your time to right each time new function in model


*****  calling function from controller   *****

    $table="tablename";
    $alias='*';
    $joinArgument=array
                (
                            array('tableName'=>'yourtablename',
                              'joinCondition'=>'firsttable.column =
                             secondtable.column',
                              'joinType'=>'LEFT'
                             ),
                  );
    $where=array('column1'=>'value');

    $data=$this->commonmodel->getData($alias,$table,$where,$joinArgument);


************ code for model 

 function getData($alias='*',$table,$where='',$join='',$orderBy='',$groupby='',$limit='')
    {
    
        $this->db->select($alias);
        $this->db->from($table);
        if($join!='')
        {
             for($i=0;$i<count($join);$i++)
             {
                 $this->db->join($join[$i]['tableName'],$join[$i
                 ['joinCondition'],$join[$i]['joinType']);
            }
        }
        if($where!='')
        {
            foreach($where as $coulmn=>$value){
                $this->db->where($coulmn,$value);
            }
        }
        if($orderBy!='')
        {
            $orderByKey=array_keys($orderBy);
            $orderByMode=array_values($orderBy);
            $this->db->order_by($orderByKey[0],$orderByMode[0]);
        }
        if($groupby != '')
        {
            $this->db->group_by($groupby);
        }
       if($limit!='')
        {
            $limit=explode(',', $limit);
            if(count($limit)>=2)
            {
                $this->db->limit($limit[1],$limit[0]);
            }else {
                $this->db->limit($limit);
            }
        }
       
        $query=$this->db->get();
        $result=$query->result();
         return $result;
    }

 public function updateData($table,$valuesArray,$whereArray){
       
        return $this->db->update($table, $valuesArray, $whereArray);
    }

public function deleteData($table,$field,$values)
    {
        $this->db->where_in($field, $values);
        $this->db->delete($table);
         return TRUE;
    }
 public function insertData($table,$insertArray)
    {
        $this->db->insert($table, $insertArray);
        return mysql_insert_id();
    } 

Comments

Popular posts from this blog

Connect multiple database and call in code igniter

Pay using braintree

Call CI controler functiona from View using ajax