• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
codeigniter

#1
Hi I want to make copy(duplicate) a record in same page and updated in database too using codeigniter from model.table id haulers.I get company(id) as dropdown of company name ,but not the other fields.Can u help me out to get solution.here is my code.

function copy_hauler($hauler_id)
{

$this->db->select('company,hauler_name,hauler_address_1,hauler_city,hauler_state,hauler_zipcode,hauler_phone,hauler_fax,hauler_email,hauler_website,hauler_contact_name,hauler_contact_number,request_email,request_text');
$this->db->from('haulers');
$this->db->where('hauler_id',$hauler_id);
$query= $this->db->get();

$following=array(
'company' =>set_value('company'),
'hauler_name' =>set_value('hauler_name'),
'hauler_address_1' =>set_value('hauler_address_1'),
'hauler_city' =>set_value('hauler_city'),
'hauler_state' =>set_value('hauler_state'),
'hauler_zipcode' =>set_value('hauler_zipcode'),
'hauler_phone' =>set_value('hauler_phone'),
'hauler_fax' =>set_value('hauler_fax'),
'hauler_email' =>set_value('hauler_email'),
'hauler_website' =>set_value('hauler_website'),
'hauler_contact_name' =>set_value('hauler_contact_name'),
'hauler_contact_number' =>set_value('hauler_contact_number'),
'request_email' =>$this->input->post('request_email'),
'request_text' =>$this->input->post('request_text'),
);


$this->db->insert('haulers',$following);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}

return FALSE;


}
Reply

#2
set_value() is meant to be used in a view, not a controller.

If you're submitting the data in a POST from a form, you should retrieve the data from $this->input->post(), as you've attempted with request_email and request_text.

If you just want to retrieve the data from the database based on an ID, as implied by your query at the beginning of the method, then you should fill your array from the query result, rather than attempting to fill it from $this->input->post().

Below are two (untested) examples, the first pulling the data from the POST, the second pulling it from the database.


PHP Code:
   // Insert the data submitted by the form
 
   public function copy_hauler($hauler_id)
 
   {
 
       // Validate the form
 
       $this->form_validation->set_rules($this->getValidationRules());
 
       if ($this->form_validation->run() === false) {
 
           return false;
 
       }

 
       // Get the POST data
 
       $following = array(
 
           'company'               => $this->input->post('company'),
 
           'hauler_name'           => $this->input->post('hauler_name'),
 
           'hauler_address_1'      => $this->input->post('hauler_address_1'),
 
           'hauler_city'           => $this->input->post('hauler_city'),
 
           'hauler_state'          => $this->input->post('hauler_state'),
 
           'hauler_zipcode'        => $this->input->post('hauler_zipcode'),
 
           'hauler_phone'          => $this->input->post('hauler_phone'),
 
           'hauler_fax'            => $this->input->post('hauler_fax'),
 
           'hauler_email'          => $this->input->post('hauler_email'),
 
           'hauler_website'        => $this->input->post('hauler_website'),
 
           'hauler_contact_name'   => $this->input->post('hauler_contact_name'),
 
           'hauler_contact_number' => $this->input->post('hauler_contact_number'),
 
           'request_email'         => $this->input->post('request_email'),
 
           'request_text'          => $this->input->post('request_text'),
 
       );

 
       // Insert, return true on success, false on failure.
 
       $this->db->insert('haulers'$following);
 
       return $this->db->affected_rows() == '1';
 
   }

 
   // Insert the data retrieved from the database
 
   public function db_copy_hauler($hauler_id)
 
   {
 
       $hauler_id = (int) $hauler_id;

 
       // Get the data for the provided ID
 
       $this->db->select('company,hauler_name,hauler_address_1,hauler_city,hauler_state,hauler_zipcode,hauler_phone,hauler_fax,hauler_email,hauler_website,hauler_contact_name,hauler_contact_number,request_email,request_text');
 
       $this->db->from('haulers');
 
       $this->db->where('hauler_id'$hauler_id);

 
       $query $this->db->get();
 
       if ($query->num_rows() < 1) {
 
           // No data found, invalid ID?
 
           return false;
 
       }

 
       // Get the first row of data, then populate $following for 
 
       // insert. You could just send $row, but you may want to do
 
       // something else with the data, first.
 
       $row $query->row_array();
 
       $following = array();
 
       foreach ($row as $key => $value) {
 
           $following[$key] = $value;
 
       }

        // Insert, return true on success, false on failure.
 
       $this->db->insert('haulers'$following);
 
       return $this->db->affected_rows() == '1';
 
   }

    // http://www.codeigniter.com/user_guide/libraries/form_validation.html#setting-rules-using-an-array
 
   protected function getValidationRules()
 
   {
 
       return array(
 
           array(
 
               'field' => 'company',
 
               'label' => 'Company',
 
               'rules' => 'max_length[255]'
 
           ),
 
       );
 
   
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.