2

I need to make Unique Serial number SN in my form. I've tried to do it, but I can't understand how this function works. Please explain clearly as I'm new to programming with codeigniter

view:my view (form)

Branch: <input type="text"  name="branch" /><br/>
Business Unit: <input type="text"  name="buinessUnit" /><br/>
Device Type: <input type="text"  name="deviceType" /><br/>
Brand: <input type="text"  name="brand" /><br/>
Device Model: <input type="text"  name="deviceModel" /><br/>
SN: <input type="text"  name="SN" /><br/>  //that i need unique
status: <input type="text"  name="status" /><br/>
department: <input type="text"  name="department" /><br/>
username: <input type="text"  name="username" /><br/>
notes: <input type="textarea"  name="notes" /><br/>
computername: <input type="text"  name="computerName" /><br/>
Save:<input type="submit"  name="save" />

</form>
</body>
</html>

model:to insert data into database

<?php
class add_model extends CI_Model {

       public function insert_into_db(){
           $post=$this->input->post(); 
           //insert data with query builder
           $data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
           $this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql
       }
}

controller:to handle model and view

<?php

class Speed extends CI_Controller {

        function insert_to_db()
           {
               $this->load->helper(array('form', 'url'));
              $this->load->library('form_validation');
               $this->form_validation->set_rules('branch', 'Branch', 'trim|required|xss_clean');
               $this->form_validation->set_rules('buinessUnit', 'BuinessUnit', 'trim|required|xss_clean');
               $this->form_validation->set_rules('deviceType', 'DeviceType', 'trim|required|xss_clean');
               $this->form_validation->set_rules('brand', 'Brand', 'trim|required|xss_clean');
               $this->form_validation->set_rules('deviceModel', 'DeviceModel', 'trim|required|xss_clean');
               $this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean');
               $this->form_validation->set_rules('status', 'Status', 'trim|required|xss_clean');
               $this->form_validation->set_rules('department', 'Department', 'trim|required|xss_clean');
               $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
               $this->form_validation->set_rules('notes', 'Notes', 'trim|required|xss_clean');
               $this->form_validation->set_rules('computerName', 'ComputerName', 'trim|required|xss_clean');
            /*   

           */
           if ($this->form_validation->run() == FALSE)
             {
                $this->load->view('pages/home');
             }
           else
             {
                 $this->load->model('add_model');
             $this->add_model->insert_into_db();
             $this->load->view('pages/home');//loading success view
            //$this->load->view('pages/formsuccess');
             }
           }

    // public function username_check($str)
    // {
        // if ($str == 'test')
        // {
            // $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            // return FALSE;
        // }
        // else
        // {
            // return TRUE;
        // }
    // }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mohamed Hamed
  • 59
  • 3
  • 11
  • set database column unique in which you want to store SN field value – codex May 30 '15 at 09:14
  • cogeigniter 3 supports rule is_unique[tablename.field] – splash58 May 30 '15 at 09:32
  • use SerialNumber for what ? For unique record storing and then to retrieve it according the id ? If so, you can ignore the Column of (SN), let the db help you to generate one. – Lyfing May 30 '15 at 10:10

4 Answers4

1

check the link.

instead of user_name use serial no. it will work for both insert and update.

Community
  • 1
  • 1
parth
  • 1,803
  • 2
  • 19
  • 27
1

Replace Your this line,

$this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean');

With,

$this->form_validation->set_rules('SN', 'SN', 'trim|required|xss_clean|is_unique[hardware_assets.SN]');
Priyank
  • 1,009
  • 12
  • 35
0

If you create your Own Function for UNIQUE SN then Used three Thing

$date = date("ymdhis");
 $SD = rand(10,2000);
$ID =$this->db->insert_id();
$SN = $ID.SD.$date; 
0

For Making Your Custom validation please add a file in your library folder my_form_validation.php as you can modify this function or create new one as per your requirement,

    <?php

    class MY_Form_validation extends CI_Form_validation {
        //used to check unique value at update record
        function unique($value, $params) {
            $CI = & get_instance();
            $CI->load->database();
            $CI->form_validation->set_message('unique', 'The %s is already taken.');
            list($table, $field, $id_field, $id) = explode(".", $params, 4);

            $data = $CI->db->select($field)->from($table)
                            ->where($field, $value)
                            ->where($id_field . " != ", $id)
                            ->get()->num_rows();
            if ($data) {
                return FALSE;
            } else {
                return TRUE;
            }
        }
}
?>
Priyank
  • 1,009
  • 12
  • 35