Update an array into database in Codeigniter - php

For my codes, When I select the checkbox, it will retrieve the date and price from database and do a calculation to get a total price.
But it update all rows with the same value.
I had tried to select 3 checkbox, and var_dump the total price, and the results are correct.
But I want to update into database, it updated all rows with the same value.
Controller:
$PledgeCheckbox = ($this->input->post('batch_pledge[]') ? '1' : '0');
//$Pledge_No = $this->input->post('batch_pledge[]'); //return value of pledge_id
if($PledgeCheckbox == '1'){
$Pledge_No = $this->input->post('batch_pledge[]'); //return value of pledge_id
//take the stg_fee_per_mth from database and calculate $Total_Stg_Fee
$Pledge = $this->pledge->GetPledgeStgFee($Pledge_No);
foreach($Pledge as $row){
$Stg_fee_per_mth = $row->stg_fee_per_mth;
$Pledge_date = $row->pledge_date;
$DATE_NOW = strtotime($Cur_date);
$DATE_PREV = strtotime($Pledge_date);
$Year1 = date('y', $DATE_NOW);
$Year2 = date('y', $DATE_PREV);
$Month_Now = date('m', $DATE_NOW);
$Month_Prev = date('m', $DATE_PREV);
$Month_diff = (($Year1 - $Year2)*12)+($Month_Now - $Month_Prev);
if($Month_diff !== 0){
$Total_Stg_Fee = $Stg_fee_per_mth * $Month_diff;
//update $Total_Stg_fee in database
$this->pledge->UpdatePledgeTotalStgFee($Total_Stg_Fee,$Month_diff);
//var_dump($Total_Stg_Fee);
}else{
//if $Month_diff == 0, $Stg_fee_per_mth must multiple with 1, if not $Total_stg_Fee will become 0
$Month_diff = 1;
$Total_Stg_Fee = $Stg_fee_per_mth * 1;
//update $Total_Stg_fee in database
$this->pledge->UpdatePledgeTotalStgFee($Total_Stg_Fee,$Month_diff);
//var_dump($Total_Stg_Fee);
}
}
$data['pledge'] = $this->pledge->FetchPledge2Renew($Pledge_No);
$this->load->view('auth/header',$data);
$this->load->view('pledge/batch_renew_pledge',$data);
$this->load->view('pledge/footer',$data);
}
Model:
public function UpdatePledgeTotalStgFee($Total_Stg_Fee,$Month_diff){
$post = $this->input->post();
//$Month_diff = $post;
//$Total_Stg_Fee = $post;
foreach($post['batch_pledge'] as $k => $value){
$TotalStgFee[] = array(
'pledge_id' => $value,
'no_of_mth' => $Month_diff,
'total_stg_fee' => $Total_Stg_Fee
);
}
$this->db->update_batch('pledge',$TotalStgFee,'pledge_id'); //('table','array','condition')
}
output of var_dump($TotalStgFee)
C:\wamp64\www\kde\store\application\models\Pledge_model.php:683:
array (size=2)
0 =>
array (size=3)
'pledge_id' => string '27' (length=2)
'no_of_mth' => int 1
'total_stg_fee' => float 20
1 =>
array (size=3)
'pledge_id' => string '21' (length=2)
'no_of_mth' => int 1
'total_stg_fee' => float 20
C:\wamp64\www\kde\store\application\models\Pledge_model.php:683:
array (size=2)
0 =>
array (size=3)
'pledge_id' => string '27' (length=2)
'no_of_mth' => int 36
'total_stg_fee' => float 900
1 =>
array (size=3)
'pledge_id' => string '21' (length=2)
'no_of_mth' => int 36
'total_stg_fee' => float 900

To iterate a posted checkbox input, it's better to use foreach instead of for loop since the $post['pledge_id'] array index is not always sequential. I've changed your codes to just call the update method only once after the foreach loop :
Controller :
$PledgeCheckbox = ($this->input->post('batch_pledge[]') ? '1' : '0');
//$Pledge_No = $this->input->post('batch_pledge[]'); //return value of pledge_id
if($PledgeCheckbox == '1'){
$Pledge_No = $this->input->post('batch_pledge[]'); //return value of pledge_id
//take the stg_fee_per_mth from database and calculate $Total_Stg_Fee
$Pledge = $this->pledge->GetPledgeStgFee($Pledge_No);
if (!empty($Pledge)) {
$Total_Stg_Fee = [];
$Month_diff = [];
foreach($Pledge as $k => $row){
$Stg_fee_per_mth = $row->stg_fee_per_mth;
$Pledge_date = $row->pledge_date;
$DATE_NOW = strtotime($Cur_date);
$DATE_PREV = strtotime($Pledge_date);
$Year1 = date('y', $DATE_NOW);
$Year2 = date('y', $DATE_PREV);
$Month_Now = date('m', $DATE_NOW);
$Month_Prev = date('m', $DATE_PREV);
$Month_diff[$k] = (($Year1 - $Year2)*12)+($Month_Now - $Month_Prev);
if($Month_diff[$k] !== 0){
$Total_Stg_Fee[$k] = $Stg_fee_per_mth * $Month_diff[$k];
//update $Total_Stg_fee in database
// $this->pledge->UpdatePledgeTotalStgFee($Total_Stg_Fee,$Month_diff);
//var_dump($Total_Stg_Fee);
}else{
//if $Month_diff == 0, $Stg_fee_per_mth must multiple with 1, if not $Total_stg_Fee will become 0
$Month_diff[$k] = 1;
$Total_Stg_Fee[$k] = $Stg_fee_per_mth * 1;
//update $Total_Stg_fee in database
// $this->pledge->UpdatePledgeTotalStgFee($Total_Stg_Fee,$Month_diff);
//var_dump($Total_Stg_Fee);
}
}
$this->pledge->UpdatePledgeTotalStgFee($Pledge_No,$Total_Stg_Fee,$Month_diff);
}
$data['pledge'] = $this->pledge->FetchPledge2Renew($Pledge_No);
$this->load->view('auth/header',$data);
$this->load->view('pledge/batch_renew_pledge',$data);
$this->load->view('pledge/footer',$data);
}
Model :
public function UpdatePledgeTotalStgFee($Pledge_No,$Total_Stg_Fee,$Month_diff){
// $post = $this->input->post();
//$Month_diff = $post;
//$Total_Stg_Fee = $post;
// $arraySize = count($post['pledge_id']);
/*
for($k=0; $k<$arraySize; $k++){
$TotalStgFee[] = array(
'pledge_id' => $post['pledge_id'][$k],
'no_of_mth' => $Month_diff.[$k],
'total_stg_fee' => $Total_Stg_Fee.[$k]
);
}
*/
$TotalStgFee = [];
foreach($Pledge_No as $k => $value){
$TotalStgFee[$k] = array(
'pledge_id' => $value,
'no_of_mth' => $Month_diff[$k],
'total_stg_fee' => $Total_Stg_Fee[$k]
);
}
$this->db->update_batch('pledge',$TotalStgFee,'pledge_id'); //('table','array','condition')
}
This will save each $Total_Stg_Fee and $Month_diff as an array first, then insert it as batch insert. I've also added $Pledge_No as a parameter on the UpdatePledgeTotalStgFee() function and passing it to the model.

Related

calcultion based on items minus stock value php

I have a calcultion based on items minus stock value (with some community help). If it hits zero then the next row will be touched.
$items = 45;
$stockRows = [40, 50, 60];
$newStock = function ($items, $stock) {
foreach ($stock as &$item) {
$delta = $item - $items;
$item = $delta > 0 ? $delta : 0;
$items = $delta < 0 ? abs($delta) : 0;
}
return $stock;
};
print_r($newStock($items, $stockRows));
Output
Array
(
[0] => 0
[1] => 45
[2] => 60
)
Now I want to add a row identifier so that I can update the database rows afterwards in a foreach and set the correct stock amount
$stockRows =
array(
array(
'id' => 1,
'amount' => 30
),
array(
'id' => 2,
'amount' => 40
),
array(
'id' => 3,
'amount' => 50
)
);
I cant get this to work. And if I get a output the first array result returns id 0.
Fixed it myself
$items = 45;
$ids = [1, 2, 3];
$stockRows = [40, 50, 60];
$newStock = function ($afschrijving, $stock, $ids) {
$i = 0;
foreach ($stock as &$item) {
$delta = $item - $afschrijving;
$item = array($id[$i++], $delta > 0 ? $delta : 0);
$afschrijving = $delta < 0 ? abs($delta) : 0;
}
return $stock;
};
print_r($newStock($items, $stockRows, $ids));

Pointer of Array conditional sum qty

I have a question about how to make an iteration. I want to place a total row after each item in the array if the next element in the array matches a specific condition. Spesific conditions have logic like this
the data like this
if i request a qty for example = 60 the result i hope like this
you can see
data[2] = 01/03/2020 just took 10 out of 40
$iter = new \ArrayIterator($values);
$sum = 0;
foreach($values as $key => $value) {
$nextValue = $iter->current();
$iter->next();
$nextKey = $iter->key();
if(condition) {
$sum += $value;
}
}
dd($iter);
how to make this logic work on php language/ laravel?
Following logic might help you on your way:
<?php
$stock = [
'01/01/2020' => 20,
'01/02/2020' => 30,
'01/03/2020' => 40
];
showStatus($stock, 'in stock - before transaction');
$demand = 60;
foreach ($stock as $key => $value) {
if ($value <= $demand) {
$stock[$key] = 0;
$supplied[$key] = $value;
$demand -= $value;
} else {
$stock[$key] -= $demand;
$supplied[$key] = $value - ($value - $demand);
$demand = 0;
}
}
showStatus($supplied, 'supplied');
showStatus($stock, 'in stock - after transaction');
function showStatus($arr = [], $msg = '')
{
echo $msg;
echo '<pre>';
print_r($arr);
echo '</pre>';
}
?>
**Output:**
in stock - before transaction
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 40
)
supplied
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 10
)
in stock - after transaction
Array
(
[01/01/2020] => 0
[01/02/2020] => 0
[01/03/2020] => 30
)
Working demo
I'm not sure I've understood you correctly but this might help:
$values = [
'01/01/2020' => 20,
'01/02/2020' => 30,
'01/03/2020' => 40
];
$demand = 60;
$total = array_sum($values);
$decrease = $total - $demand; //(20+30+40) - 60 = 30
$last_key = array_keys($values,end($values))[0]; //Is 01/03/2020 in this case
$values[$last_key] -= $decrease; //Decrease value with 30 calulated above
Would output:
Array
(
[01/01/2020] => 20
[01/02/2020] => 30
[01/03/2020] => 10
)

How to retrieve the date of the working days in a php array?

Can someone help me, where should I put my $days[] array so that holidays are not included in this array.
My code is as follows
$data['ferie'] = $this->CalendrierModel->find('ferie');
$timestamp = strtotime ('2020-05-18');
$days = array();
$i = 0;
$jourOuvre = 5;
foreach($data['ferie'] as $row){
$ferie = array($row->start);
while($i < $jourOuvre){
$date_tmp = date("Y-m-d", strtotime($i . 'weekdays', $timestamp));
if (in_array($date_tmp , $ferie)){
$jourOuvre++;
$date_tmp = date("Y-m-d", strtotime('+1days', $date_tmp));
}
$days[] = $date_tmp;
$i++;
}
}
var_dump($days);
this code makes :
699:
array (size=5)
0 => string '2020-05-18' (length=10)
1 => string '2020-05-19' (length=10)
2 => string '2020-05-20' (length=10)
3 => string '2020-05-21' (length=10)
4 => string '2020-05-22' (length=10)
while 2020-05-21 is a holiday.
NB: $data['ferie'] is an array of holidays.
Thanks, guys. I did it.
$data['ferie'] = $this->CalendrierModel->find('ferie');
$timestamp = strtotime ('2020-05-18');
$days = array();
$ferie = array();
$i = 0;
$jourOuvre = 5;
foreach($data['ferie'] as $row){
$ferie[] = $row->start;
}
while($i < $jourOuvre){
$date_tmp = date("Y-m-d", strtotime($i . ' weekdays' , $timestamp));
if(in_array($date_tmp , $ferie)) {
$jourOuvre++;
} else {
$days[] = $date_tmp;
}
$i++;
}
var_dump($days);
Holidays are out of my array :
C:\wamp\www\HELPJUR\application\controllers\Ticket.php:816:
array (size=5)
0 => string '2020-05-18' (length=10)
1 => string '2020-05-19' (length=10)
2 => string '2020-05-20' (length=10)
3 => string '2020-05-22' (length=10)
4 => string '2020-05-25' (length=10)

adding elements to the array creates a new array instead of adding

i have been trying to add two elements to my array which is always been created. but after i add those elements it creates new element instead adding to the existing one.
code
<?php
Continues from top....
while($finalRes = mysql_fetch_assoc($excute))
{
$tables[] = $finalRes;
}
if(mysql_num_rows($excute) != 0){
$report = new Report();
$ID = substr($table,11);
$log = $report->projecteden($ID);
$a=0;
$b=0;
if(property_exists($log, 'counts')){
foreach ($log->counts as $m)
{
$UNIQUES = $m->count;
$NON-UNIQUES = $m->ucount;
$a += $UNIQUES;
$b += $NON-UNIQUES;
}
$tables['UNIQUES'] = $a;
$tables[] = $tables['UNIQUES'];
$tables['NON-UNIQUES'] = $b;
$tables[] = $tables['NON-UNIQUES'];
}
}
var_dump($tables);
?>
var_dump result
array (size=3)
0 =>
array (size=8)
'ID' => string '105' (length=3)
'name' => string 'R158' (length=11)
'accountname' => string 'DDD' (length=3)
'accountID' => string '1' (length=1)
'stat' => string '2' (length=1)
'total_impr' => string '207' (length=3)
'min(a.timestamp)' => string '2014-05-16 05:38:01' (length=19)
'max(a.timestamp)' => string '2015-01-22 05:50:41' (length=19)
'UNIQUES' => int 45
'NON-UNIQUES' => int 13
as u can see UNIQUES and NON-UNIQUES are not aligned, can any help me to add UNIQUES and NON-UNIQUES to the same $tables array
Assign index like this
your code....
$tables[0]['UNIQUES'] = $a;
$tables[0]['NON-UNIQUES'] = $b;
your code ....
if you want to reemove 0 index then
$tables = $tables[0];
Try with -
while($finalRes = mysql_fetch_assoc($excute)){
$temp = $finalRes;
if(mysql_num_rows($excute) != 0){
$report = new Report();
$ID = substr($table,11);
$log = $report->projecteden($ID);
$a=0;
$b=0;
if(property_exists($log, 'counts')){
foreach ($log->counts as $m)
{
$UNIQUES = $m->count;
$NON-UNIQUES = $m->ucount;
$a += $UNIQUES;
$b += $NON-UNIQUES;
}
array_merge($temp ,array('UNIQUES' => $a));
array_merge($temp ,array('NON-UNIQUES' => $b));
$tables[] = $temp;
}
}
}
var_dump($tables);
1) you can create new temp array and add element to that array.
2) use array_merge to merge element of temp array to our main array with key & value pair.
3) use can use below code. it is working fine.
while($finalRes = mysql_fetch_assoc($excute))
{
$tables[] = $finalRes;
}
if(mysql_num_rows($excute) != 0){
$report = new Report();
$ID = substr($table,11);
$log = $report->projecteden($ID);
$a=0;
$b=0;
if(property_exists($log, 'counts')){
foreach ($log->counts as $m){
$UNIQUES = $m->count;
$NON-UNIQUES = $m->ucount;
$a += $UNIQUES;
$b += $NON-UNIQUES;
}
$temp = array('UNIQUES' => $a,'NON-UNIQUES' => $b);
$tables = array_merge($tables,$temp);
}
}
var_dump($tables);

Select smallest and biggest value of an object

I'm trying to get the smallest value of start and the biggest value of end outoff the object below. The object is an result of a sql script.
object(CI_DB_mysql_result)[18]
public 'conn_id' => resource(54, mysql link persistent)
public 'result_id' => resource(61, mysql result)
public 'result_array' =>
array (size=0)
empty
public 'result_object' =>
array (size=3)
0 =>
object(stdClass)[16]
public 'id' => string '601' (length=3)
public 'scheduled' => string '0' (length=1)
public 'start' => string '2014-10-17 06:00:00' (length=19)
public 'end' => string '2014-10-17 11:00:00' (length=19)
1 =>
object(stdClass)[19]
public 'id' => string '602' (length=3)
public 'scheduled' => string '0' (length=1)
public 'start' => string '2014-10-17 18:00:00' (length=19)
public 'end' => string '2014-10-17 19:30:00' (length=19)
2 =>
object(stdClass)[20]
public 'id' => string '603' (length=3)
public 'scheduled' => string '1' (length=1)
public 'start' => string '2014-10-17 11:00:00' (length=19)
public 'end' => string '2014-10-17 18:00:00' (length=19)
public 'custom_result_object' =>
array (size=0)
empty
public 'current_row' => int 0
public 'num_rows' => int 3
public 'row_data' => null
In this case the smallest value of start = '2014-10-17 06:00:00' and the biggest value of end = '2014-10-17 19:30:00'. So the result I'm looking for looks like this:
$smallest_start_value = '2014-10-17 06:00:00';
$biggest_end_value = '2014-10-17 19:30:00';
How should the code look like, in order to compute this result?
Function:
function unschedule_resource() {
$event_id = 4;//$_POST['event_id'];
$event_start = '2014-10-17 11:00:00';//$_POST['event_start'];
$event_end = '2014-10-17 18:00:00';//$_POST['event_end'];
// Get resource id
$this->db->select('
event.resource_id'
);
$this->db->from('promo_manager.event');
$this->db->where('event.id =', $event_id);
$data = $this->db->get();
foreach ($data->result() as $row) {
echo $row->resource_id . "<br>";
$resource_id = $row->resource_id;
}
// Get resource events to unschedule
$this->db->select('
resource_calendar.id,
resource_calendar.scheduled,
resource_calendar.start,
resource_calendar.end'
);
$this->db->from('promo_manager.resource_calendar');
$this->db->where('resource_calendar.resource_id =', $resource_id);
$this->db->where('resource_calendar.start =', $event_start);
$this->db->where('resource_calendar.end =', $event_end);
$this->db->or_where('resource_calendar.end =', $event_start);
$this->db->or_where('resource_calendar.start =', $event_end);
$data = $this->db->get();
$lowest_value = null;
$highest_value = null;
foreach ($data as $the_key => $the_value) {
if ($the_key == 'start') {
if ($lowest_value === null) {
$lowest_value = $the_value;
}
if ($the_value < $lowest_value) {
$lowest_value = $the_value;
}
}
}
//echo $lowest_value;
var_dump ($lowest_value) ;
}
The code of Sam should look like this:
$lowest_value = null;
$highest_value = null;
foreach($data as $obj) { //$data should be the array containing the result objects.
$start = $obj->start;
$end = $obj->end;
if ($lowest_value === null)
$lowest_value = $start;
else
if ($lowest_value > $start)
$lowest_value = $start;
if ($highest_value === null)
$highest_value = $end;
else
if ($highest_value < $end)
$highest_value = $end;
}
can be shortened to :
if ($lowest_value === null || $lowest_value > $start)
$lowest_value = $start;
if ($highest_value === null || $highest_value < $end)
$highest_value = $end;
Both values can be generated within one iteration without problems. Keep in mind that the < and > works in the case you keep the date formated as given. Otherwhise you would need to use date-functionality to compare the dates.
You could also use MySQL to query directly for the Min/Max values using the Aggregation methods Min or Max: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
(If you don't need each object to be loaded this will be faster)
I'm not sure if there's a way you couldn't do this in the select statement or not, I'll assume not. Get your result object into an array. This is super basic but should get the job done.
$lowest_value = null;
$highest_value = null;
foreach(result_object as $the_key => $the_value) {
if($the_key == 'start') {
if($lowest_value === null) {
$lowest_value = $the_value;
}
if(the_value < $lowest_value) {
$lowest_value = $the_value;
}
}
}
Do a second check for the end value just changing the operator

Categories