简体   繁体   中英

How to insert multiple dynamic data using CodeIgniter?

I am using CodeIgniter, I am displaying the fields dynamically. Now I have to insert the data in the database. So I tried below code.

$order = $this->input->post('order[]');
$partner = $this->input->post('parner[]');
$bankname = $this->input->post('newpartner[]');
$status = $this->input->post('filestatus[]');
$user_id = $this->input->post('user_id');

$order_length = sizeof($order);

for ($j = 0; $j < $order_length; $j++) {

  if (($status = 1) || ($status = 3)) {
    $remark = $this->input->post('remark[]');
  } else {
    $remark = "";
  }

  if (($status = 2) || ($status = 4))) {
  $reasonDate = $this->input-> post('reasonDate[]');
  $remark = $this->input-> post('remark[]');
} else {
  $reasonDate = "";
  $remark = "";

}

if ($status = 7) {
  $reasonAmt = $this->input->post('reasonAmt[]');
  $reason = $this->input->post('reason[]');
} else {
  $reasonAmt = "";
  $reason = "";
}


$data['row'] = array(
  'order' => $order[$j],
  'bankname' => $bankname[$j],
  'status' => $status[$j],
  'lead_id' => $user_id,
  'remark' => $remark[$j],
  'reasonDate' => $reasonDate[$j],
  'reasonAmt' => $reasonAmt[$j],
  'reason' => $reason[$j]
);
$save = array(
  'b_orderno' => $data['row']['order'],
  'b_bankname' => $data['row']['bankname'],
  'b_filestatus' => $data['row']['status'],
  'p_id' => $data['row']['pid'],
  'lead_id' => $data['row']['lead_id'],
  'b_remark' => $data['row']['remark'],
  'b_date' => $data['row']['reasonDate'],
  'b_amt' => $data['row']['reasonAmt'],
  'b_reason' => $data['row']['reason']
);

$afterxss = $this->security-> xss_clean($save);
if ($afterxss) {
  $this - > db - > insert('tbl_bankdata', $afterxss);
  $response['error'] = "true";
  $response['msg'] = "added successfully";

} else {
  $response['error'] = "false";
  $response['msg'] = "Sometning wrong! please check the internet connection and try again";
}

}
echo json_encode($response);

I am getting the issue on the status field because depending upon the status value input field will display. Also, I used If the condition in logic for the status field. Each row has a unique status field.

You will find my HTML and script in below link.

https://jsfiddle.net/08phzue3/

This is my UI screenshot. Ignore the value that is only for testing purpose.

1)Onload this will display 在此处输入图片说明 2) If user select the status 在此处输入图片说明 3) If multiple row 在此处输入图片说明

Would you help me out with this?

Actually you are not comparing here:

if (($status = 1) || ($status = 3)) {

You are assigning the values to your $status variable

this should be:

if (($status == 1) || ($status == 3)) {

Side note: same for other conditions as well.

Edit:

As you mentioned you are getting array in $status so using comparison is not a correct logic here, there are multiple solution available,

You can use in_array() like:

if(in_array(1, $status) || in_array(3, $status)){
    $remark = $this->input->post('remark[]');   
}
else{
    $remark = "";   
}

Final Solutions (04-07-2019):

After changing the $save and $data arrays, issue has been resolved for OP:

$save['b_orderno'] = $order[$j]; 
$save['b_bankname'] = $bankname[$j]; 
$save['b_filestatus'] = $status[$j]; 
$save['p_id'] = $partner[$j]; 
$save['lead_id'] = $user_id; 


if(!empty($remark[$j])){ 
$save['b_remark'] = $remark[$j]; 
} 
if(!empty($reasonDate[$j])){ 
$save['b_date'] = $reasonDate[$j]; 
} 
if(!empty($message[$j])){ 
$save['b_message'] = $message[$j]; 
} 
if(!empty($reasonAmt[$j])){ 
$save['b_amt'] = $reasonAmt[$j]; 
} 
if(!empty($reason[$j])){ 
$save['b_reason'] = $reason[$j]; 
}


$data['row']['order'] = $order[$j]; 
$data['row']['bankname'] = $bankname[$j]; 
$data['row']['status'] = $status[$j]; 
$data['row']['lead_id'] = $user_id; 
$data['row']['remark'] = $remark[$j]; 
$data['row']['reasonDate'] = $reasonDate[$j]; 

if(!empty($reasonAmt[$j])){ 
$data['row']['reasonAmt'] = $reasonAmt[$j]; 
} 
if(!empty($reason[$j])){ 
$data['row']['reason'] = $reason[$j]; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM