Store array and update record in database using codeigniter

Multi tool use
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.
2 Answers
2
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");
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
@blacmon I am not understand your point please explain more.
– falak hamid
Sep 25 '15 at 20:53