Sunday 6 March 2016

Create Auto Increment without setting in PHPMYADMIN using CI

well may be the title make you hard to understand, let me give you example of my work, please see my images bellow with circle in that form

well logic is i select id from table order by desc limit 1 then in view i add with number 1 then i will look like autoincrement but it's not, here is my database screen shoot

then in view show up with number 10, so let me show my code,
cCONTROLLER
 $data = array(  
                //pro_employee_id auto increment  
                'id'=>$this->Karyawan_model->getlastid(),  
                );  
 $this->template->load('template', 'pro_employee_form',$data);  

MODEL
 function getlastid()  
      {  
        $this->db->from('pro_employee');  
     $this->db->order_by("pro_employee_id", "desc");  
     $this->db->limit('1');   
     return $this->db->get()->row();    
      }  

VIEW 
 <?php  
           $maxid = 0;  
     if ($id) {  
       $maxid = $id->pro_employee_id;   
     }   
           ;?>  
            <input type="text" class="form-control" name="pro_employee_id" id="pro_employee_id" value="<?php echo $maxid+1 ?>" readonly />  

hope you understand what i'm telling in here, thanks for visiting me,
Cheers


No comments:

Post a Comment