简体   繁体   中英

Store array and update record in database using codeigniter

I am storing dynamic array as well as update the "max_quantity" table in database. The problem is that when update the record than it will sum all the quantity values and subtract it from the 1st value of "max quantity table". But I want that array of quantity is subtracted its own quantity value which is present in the database.

This is my code in view

<?php $i=1; foreach($result as $row){
        ?>

  <td class="pr-right" style='width:130px; text-align: center; '>

                <input type="text" min="1" step="1"  name="quantity[]" step="1" class="container" value="" onfocus="this.value = '';" onblur=";" style="width: 60px" id="quantityT<?php echo $i;?>" onkeyup="CalculatePrice (<?php echo $i;?>,<?php echo $row->max_quantity; ?>)">
                <br>(Quantity Available = <?php echo $row->max_quantity; ?>)
            </td>

             <?php
        $i++;
} ?>

This is my code in controller

public function get_insert_order(){
   $quantity=$this->input->post('quantity');

   for($i=0; $i<count($ingredient); $i++){
    $data = array(
    "user_quantity" =>$quantity[$i],
    );
      $response =  $this->bulk_recipe->insert_bulk_order($data);
            $max = $this->db->query("select bulk_delivery.max_quantity from bulk_delivery")->result_array();

            foreach ($max as $rows) {
                $calc = $rows['max_quantity'];
                $calc = $calc - $quantity[$i];
            }


            $this->db->query("UPDATE bulk_delivery SET max_quantity =$calc");
}

This is my code in model

function insert_bulk_order($form_data)
{
    $this->db->insert('bulk_delivery_order',$form_data);


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

    return FALSE;
}

This is front end view before updation.

在此处输入图片说明

This is front end after updation

在此处输入图片说明

Please guide me how i subtract each quantity to its max_quantity individually.

This what you are looking for...!

$this->db->set();

This function enables you to set values for inserts or updates.

It can be used instead of passing a data array directly to the insert or update functions:

$this->db->set('field', 'field+1', FALSE);
$this->db->insert('mytable'); 
// gives INSERT INTO mytable (field) VALUES (field+1)

$this->db->set('field', 'field+1');
$this->db->insert('mytable'); 
// gives INSERT INTO mytable (field) VALUES ('field+1')

https://ellislab.com/codeigniter/user-guide/database/active_record.html

Your code has couple of bugs, you are selecting all rows from db and updating all of them every time. your code should be like this...

$max = $this->db->query("select bulk_delivery.max_quantity from bulk_delivery where some_id = $i")->row_array();

$calc = $rows['max_quantity'];
$calc = $calc - $quantity[$i];

$this->db->query("UPDATE bulk_delivery SET max_quantity =$calc WHERE some_id = $i");

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