CodeIgniter Forums
error in Array to string conversion - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: error in Array to string conversion (/thread-73841.html)

Pages: 1 2 3


error in Array to string conversion - jackey - 06-11-2019

hi,
I want to insert a new row to retire table at the same time when i insert a new row to employee table.

can anyone pls tell me how to fix these error?

/////view
Code:
  <div class="form-group">
       <label class="col-sm-2 control-label">Emp No.</label> <div class="col-sm-2"> <input type="text" name="emp_no" id="emp_no" class="form-control"/></div>
       <label class="col-sm-1 control-label">NIC</label> <div class="col-sm-2"> <input type="text" name="nic" id="nic" class="form-control"/></div>
       
   </div>

<div class="form-group"> <label class="col-sm-2 control-label">DOB</label>  
   <div class="col-sm-2" ><input type="date" class="form-control" id="dob" name="dob" /></div>
</div>


////controller

Code:
$data = array(        
'emp_no' => $this->input->post('emp_no'),
'nic' => $this->input->post('nic'),
'dob' => $this->input->post('dob'),
-----etc
);

$dob=strtotime($this->input->post('dob'));
$due_date_retire = date('Y-m-d', strtotime($dob . '+60 Years'));

$data_retire_tbl = array(  
'emp_no' => $this->input->post('emp_no'),
'due_date' => $due_date_retire  
);

$this->Model_user->model_add_new_emp($data,$data_retire_tbl);
$this->session->set_flashdata('message', ' Successfully ADDED New Employee ......');
echo json_encode(array("status" => TRUE));
 

////model
Code:
public function model_add_new_emp($data, $data_retire_tbl) {  
$this->db->insert('emp_pmt', $data);
$this->db->insert('retire', $data_retire_tbl);
return $this->db->insert_id();
       }



RE: error in Array to string conversion - demyr - 06-11-2019

Within your view, I cannot see the "form" tag.

Code:
<form action="<?php echo base_url('Your_Controller/your_method')?>" method="post">

// your inputs go here

</form>


Plus, you can also add an id or sth after url

Code:
<form action="<?php echo base_url('Your_Controller/your_method/').$my_item->my_item_id; // or $this->uri->segment(2-3-4
whatever(if you have id in your url));?>" method="post">
</form>



RE: error in Array to string conversion - jackey - 06-11-2019

i already insert form tag as 
<form action="#" class="form-horizontal" id="form_new_emp"> 

// my inputs go here

<div class="form-group"><label class="col-sm-5 control-label">
          <button type="button" class="btn-success btn-sm" id="btn_add_emp" onClick="add_emp()"> &nbsp;ADD</button>
          <button type="button" class="btn-danger btn-sm" id="btn_add_cancel"> &nbsp;CANCEL</button>      
        </div>
    
</form>

Then ajax part like this
Code:
function add_emp()
   {      
 
   
   var form = $('#form_new_emp')[0];
   var formData = new FormData(form);
   
       show_loading();
         $.ajax({
           url : "<?php echo site_url('index.php/ClerkNAE/add_new_emp')?>",      
           type: "POST",
           data: formData,
           contentType: false,
           processData: false,
           dataType: "JSON",
           
           success: function(data)
           {
                if(data.status)
                       {              
                          location.reload();
                       }
                        else
                       {
                           hide_loading();
                           for (var i = 0; i < data.inputerror.length; i++)
                           {
                               $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                               $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
                           }
                       }
           },
           error: function (jqXHR, textStatus, errorThrown)
           {
               
               alert('ERROR in ADDING NEW EMPLOYEE');
           }
       });    
   }
I think the problem in these two lines .
$dob=strtotime($this->input->post('dob'));

$due_date_retire = date('Y-m-d', strtotime($dob . '+60 Years'));


RE: error in Array to string conversion - Wouter60 - 06-12-2019

Quote:I think the problem in these two lines .
$dob=strtotime($this->input->post('dob'));

$due_date_retire = date('Y-m-d', strtotime($dob . '+60 Years'));

Exactly.
Change this part to:
PHP Code:
$dob $this->input->post('dob');
$due_date_retire date('Y-m-d'strtotime($dob ' +60 years')); 

Mind the space before the +.


RE: error in Array to string conversion - jfri - 06-12-2019

This won't get you the correct results anyways because you convert the timestamp+60 Years - so it's not the correct form for the function. Try this:


PHP Code:
$dob=strtotime('2018-09-01 + 60 years');

$due_date_retire date('Y-m-d'$dob); 

Maybe this will also solve your Array problem. Maybe also try this line:
PHP Code:
$this->db->reset_query() 
so you are sure there are no leftovers from the first insert


RE: error in Array to string conversion - jackey - 06-12-2019

It is not working. it gives the same error message Sad


RE: error in Array to string conversion - demyr - 06-12-2019

Allright, can you please try seperating these 2 inserts as I suggested here


RE: error in Array to string conversion - jackey - 06-12-2019

(06-12-2019, 12:28 AM)jfri Wrote: This won't get you the correct results anyways because you convert the timestamp+60 Years - so it's not the correct form for the function. Try this:


PHP Code:
$dob=strtotime('2018-09-01 + 60 years');

$due_date_retire date('Y-m-d'$dob); 

Maybe this will also solve your Array problem. Maybe also try this line:
PHP Code:
$this->db->reset_query() 
so you are sure there are no leftovers from the first insert

i want to get the user input DOB from the form as $dob $this->input->post('dob');
i can't put exact date like 2018-09-01


RE: error in Array to string conversion - InsiteFX - 06-12-2019

Also this line in your ajax is wrong site_url returns index.php so remove from the string.

Code:
//WRONG:
url : "<?php echo site_url('index.php/ClerkNAE/add_new_emp')?>",

// Should be
url : "<?php echo site_url('ClerkNAE/add_new_emp')?>",



RE: error in Array to string conversion - jackey - 06-12-2019

(06-12-2019, 03:57 AM)InsiteFX Wrote: Also this line in your ajax is wrong site_url returns index.php so remove from the string.

Code:
//WRONG:
url : "<?php echo site_url('index.php/ClerkNAE/add_new_emp')?>",

// Should be
url : "<?php echo site_url('ClerkNAE/add_new_emp')?>",

I removed index.php
But it gives the same error Sad