2015-07-28 63 views
-1

這裏我得到的數據插入數據庫多個數據,但如何在foreach循環寫這讓多個數據請幫我作爲一個新鮮的我完全糊塗了..如何插入笨使用insert_batch

我控制器

class Student extends CI_Controller { 

    public function _construct() 
    { 
     parent::_construct(); 
     //call model 
     $this->load->model("StudentModel","m"); 
    } 

    function index() 
    { 
     $this->load->view("index"); 
    } 
    function savedata() 
    { 
     //create array for get data from index 
    //$data=array(
        // 'studentname' => $this->input->post('studentname'), 
         //'gender' => $this->input->post('gender'), 
         //'phone' => $this->input->post('phone') 
        //); 

     $data = array(
    array(
     'studentname' => 'Reddy' , 
     'gender' => 'Male' , 
     'phone' => '456879' 
    ), 
    array(
     'studentname' => 'Yalla' , 
     'gender' => 'Female' , 
     'phone' => '12345678' 
    ) 
); 


    //mean that insert into database table name tblstudent 

     $this->db->insert_batch('tblstudent',$data); 

    //mean that when insert already it will go to page index  
     redirect("Student/index"); 
    } 

    function edit($id) 
    { 
     $row=$this->m->getonerow($id); 
     $data['r']=$row; 
     $this->load->view('edit',$data); 

    } 
    function update($id) 
    { 
     $id=$this->input->post('id'); 
     $data=array(
         'studentname' => $this->input->post('studentname'), 
         'gender' => $this->input->post('gender'), 
         'phone' => $this->input->post('phone') 
        ); 
        $this->db->where('id',$id); 
        $this->db->update('tblstudent',$data); 
        redirect("Student/index"); 

    } 
    function delete($id) 
    { 
     $id=$this->db->where('id',$id); 
     $this->db->delete('tblstudent'); 
     redirect("Student/index"); 
    } 

} 

我的模型

class StudentModel extends CI_Model{ 

    function _construct() 
    { 
     parent::_construct(); 
    } 
    function gettable() 
    { 
     $query=$this->db->get('tblstudent'); 
     return $query->result(); 
    } 
    function getonerow($id) 
    { 
     $this->db->where('id',$id); 
     $query = $this->db->get('tblstudent'); 
     return $query->row(); 

    } 
} 

回答