I have a problem with my addition :
So I have this code :
$total = 0;
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
$aHistoryFilter['total_montant'] = $total+$history['montant'];
$aHistory[] = $aHistoryFilter;
}
return $aHistory;
So I want to save in total_montant the last value, but not work and I don't understand why...Can you help me please ? Thx in advance
You should also do:
$total = $total + $history['montant'];
otherwise you do not add anything (since $total=0;)
So you get:
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
$aHistoryFilter['total_montant'] = $total+$history['montant'];
$total = $total + $history['montant'];
$aHistory[] = $aHistoryFilter;
}
update your code to be:
$total = 0;
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
$total = $total+$history['montant'];
$aHistory[] = $aHistoryFilter;
}
$aHistoryFilter['total_montant'] = $total ;
because in your code you $history['montant'] to $total but you didn't assign the result to $total
Try this:
$total = 0;
foreach(getHistory($this->id) as $history){
$aHistoryFilter['date'] = $history['date'];
$aHistoryFilter['ls'] = $history['ls']);
$aHistoryFilter['montant'] = $history['montant'];
// this adds the $history['montant'] to the $total
$total += $history['montant'];
// this adds the $total to your variable
$aHistoryFilter['total_montant'] = $total;
$aHistory[] = $aHistoryFilter;
}
return $aHistory;
Related
I have problem with my variable, I wanna make variable like these
$H1,H2, until $h48
but I know is kinda difficult, so I wanna change to like these
$H[1],$H[2],$H[3] until $H[48]
these I try with my code:
$loopCount = 1;
$H = array();
for ($loopCount = 1; $loopCount<49; $loopCount=$loopCount + 1)
{
$H[$loopCount] = 0;
}
But it is not working at all. How to make it work?? Any idea??
EDIT RESOLVED : Thanks To Ghost helping me out.
Here my revisi code :
$H = array();
for ($loopCount = 1; $loopCount<49; $loopCount=$loopCount + 1)
{
$H{$loopCount} = 0;
}
And just call $H{1} , is complete work. Thanks again to Ghost you're hero :)
You could try this :
$loopCount = 1;
$H = '';
for ($loopCount = 1; $loopCount<48; $loopCount=$loopCount + 1)
{
${"H" . $loopCount} = 0;
}
I am writing a bit of php code to output a random value
$max_mal = (3 - $oray);
$oray = 1;
$max = 100;
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
and for each loop goes here
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
I am looking to get decimal values upto 5 numbers, but once a negative value comes it results out as 0.000-1 instead of -0.00001
$myresultb = str_pad($mario, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
$total_score = 300;
echo $myresultf;
Secondly I am new to php learning so am I doing this PHP correct or it needs improvement
I have a div to show total score like this
<div id="total_score"></div>
and another div to show current score which value comes as echo $myresultf;
<div id="current_score"></div>
I want to update total score in real time with jquery wheneven button is clicked and <?php echo $myresultf ?> is refreshed in real time also
$("#play").click(function() {
var currentscore = $("#current_score").val();
var totalscore = $("#total_score").val();
how to do this.....
});
Try this:
$max = 100;
$oray = 1;
$max_mal = (3 - $oray);
$total = 0;
for ($i = 0; $i < $max_mal; $i++){
$goli = mt_rand(3, 8);
$total += $goli;
$golis[] = $goli;
}
foreach($golis as &$goli) {
$goli = floor(($goli / $total) * $max);
if ($goli == 0) {
$goli = 1;
}
}
$result = array_pad($golis, 3, -1);
shuffle($result);
$myresult = $result[0];
$negative_var=false;
if($myresult < 0)
{
$negative_var=true;
$myresult = 0-$myresult;
}
$myresultb = str_pad($myresult, 5, '0', STR_PAD_LEFT);
$myresultf = '0.'.$myresultb.'<br/>';
if($negative_var)
$myresultf="-".$myresultf;
$total_score = 300;
echo $myresultf;
simple use as follow:
$myresultb =str_replace('-','',$myresultb);
if($myresult == -1) {
$myresultf = '-0.'.$myresultb.'<br/>';
}
else {
$myresultf = '0.' . $myresultb . '<br/>';
}
I made an order form using PHP that makes subtotals for each item ordered. How can I make a function/code to display the total for the order in the bottom of the page. I tried to read on PHP.net but couldn't figure it out.
Here is a sample of my code:
$bread = $_POST["bread"];
$cheese = $_POST["cheese"];
$eggs = $_POST["eggs"];
$priceBread = 5;
$priceCheese = 5;
$priceEggs = 3.6;
function subtotal($incomingQuantity, $incomingPrice){
return $incomingQuantity * $incomingPrice;
}
<div id="breadSubtotal">$<?php echo subtotal($bread, $priceBread); ?>
<div id="cheeseSubtotal">$<?php echo subtotal($cheese, $priceCheese); ?></div>
<div id="eggsSubtotal">$<?php echo subtotal($eggs, $priceEggs); ?></div>
I want the total from the subtotals of all the items
something like this?
$total = 0;
function subtotal($incomingQuantity, $incomingPrice){
global $total;
$sub = $incomingQuantity * $incomingPrice;
$total += $sub;
return $sub;
}
then in the HTML
<div id="total">$<?php echo $total; ?></div>
also, dont forget to make safe your incoming $_POST vars with something like
$bread = htmlspecialchars($_POST["bread"]);
I want the total from the subtotals of all the items
Its really simple to calculate. Try like this
$bread = 2;
$cheese = 3;
$eggs = 4;
$priceBread = 5;
$priceCheese = 5;
$priceEggs = 3.6;
$total = 0;
function subtotal($incomingQuantity, $incomingPrice){
return $incomingQuantity * $incomingPrice;
}
$total += subtotal($bread, $priceBread) ;
$total += subtotal($bread, $priceBread);
$total += subtotal($cheese, $priceCheese);
echo "order total : " + $total;
If am not wrong, you want to calculate the number of each item with the price of the items. So based on that this is how I would do it.
public function getTotal()
{
$total = 0;
$subTotal = func_get_args();
for($a = 0; $a < sizeof($subTotal); $a++)
{
$total += $subTotal[$a];
}
return $total;
}
Test the function
getTotal(subtotal($bread, $priceBread), subtotal($cheese, $priceCheese), subtotal($eggs, $priceEggs));
That's how I would do it, gives you more flexibility
<?php
$bread = $_POST["bread"];
$cheese = $_POST["cheese"];
$eggs = $_POST["eggs"];
//prices
$prices = array('bread'=>5,'cheese'=>5,'eggs'=>3.6);
function calculateOrderTotals($items, $prices){
//result
$result = array('total'=>0, 'subTotal'=>array());
//total
$total = 0;
//calculate subtotal and total
foreach ($items as $item => $nPurchased){
$subTotal = $nPurchased* $prices[$item];
$result['subTotal'][$item] = $subTotal;
$total += $subTotal;
}
//set total
$result['total'] = $total;
//return
return $result;
}
//call function to calculate
$totals = calculateOrderTotals(array('bread'=>$bread,'cheese'=>$cheese,'eggs'=>$eggs), $prices);
?>
<div id="breadSubtotal"><?php echo $totals['subTotal']['bread'];?>
<div id="cheeseSubtotal"><?php echo $totals['subTotal']['cheese']; ?></div>
<div id="eggsSubtotal"><?php echo $totals['subTotal']['eggs']; ?></div>
<div id="total"><?php echo $totals['total']; ?></div>
I have a simple problem, but I can't solve it, still.
Working code
(int)$sum_price=4;
(int)$price_per_sum = (int)$spo[$key]*(int)$gpp['price'];
echo $sum_price = ($sum_price + $price_per_sum);
When I change 4 to a variable, the result of sum() is string.
Result becomes a string
$spo[$key] = 1;
$gpp['price'] = 2;
(int)$sum_price=4;
(int)$price_per_sum = (int)$spo[$key]*(int)$gpp['price'];
echo $sum_price = ($sum_price + $price_per_sum);
The result is 42 but should be 6, instead. Using (int) does not change my result.
Problem code
# Get All Payment
(int)$sum_price = 0;
$sum_price_product = explode('|',$_SESSION['product']);
$spo = explode('|',$_SESSION['order']);
foreach($sum_price_product as $key=>$spp)
{
if($spp!='')
{
$get_product_price = $dbc->select("sh_product"," id = '{$spp}'","id");
$gpp = mysql_fetch_array($get_product_price['sql']);
(int)$price_per_sum = $spo[$key] * $gpp['price'];
$sum_price = $sum_price + $price_per_sum;
echo $sum_price;
}
}
http://codepad.org/Zo9X2PY5
Type Casting is not necessary in this case...
And please put the right associative array key its "price"or 'price' not just price...
$sum_price = 4;
$price_per_sum = $spo[$key] * $gpp['price'];
$sum_price = ($sum_price + $price_per_sum);
echo $sum_price;
And your Example :
$spo[$key] = 1;
$gpp['price'] = 2;
$sum_price = 4;
$price_per_sum = $spo[$key] * $gpp['price'];
$sum_price = ($sum_price + $price_per_sum);
echo $sum_price;
# Get All Payment
(int)$sum_price=0;
$sum_price_product = explode('|',$_SESSION['product']);
$spo = explode('|',$_SESSION['order']);
foreach($sum_price_product as $key=>$spp)
{
if($spp!='')
{
$get_product_price = $dbc->select("sh_product"," id = '{$spp}'","id");
$gpp = mysql_fetch_array($get_product_price['sql']);
(int)$price_per_sum = $spo[$key]*$gpp['price'];
$sum_price = $sum_price + $price_per_sum;
echo $sum_price;
}
}
Casting is not required in this case. Please review below code and try it out,its working for me:
$spo[0] = 1;
$gpp['price'] = 2;
$sum_price=4;
$price_per_sum = $spo[0]*$gpp['price'];
$sum_price = $sum_price + $price_per_sum;
echo $sum_price;
Here in $spo[0], i have considered single value but you can replace '0' by $key in loop construct.
Please review here my working code: http://codepad.org/zH2q2WjH
$t_enquirys_big_total = 0;
for ($i=0; $i<$totaldept; $i++){
$name = $v_ud_name[$i];
$querytotal = "SELECT category, user_dept_name
FROM $t_bug_table
WHERE team_id = '$t_team' && user_dept_name = '$name' && date_submitted BETWEEN '$t_start_string2' AND '$t_end_string2'";
$resulttotal = db_query ($querytotal);
while ($rowtotal = db_fetch_array ($resulttotal)){
$t_enquirys_big_total++;
}
}
for ($i=0; $i<$totaldept; $i++){
$name = $v_ud_name[$i];
$querybug = "SELECT category, user_dept_name
FROM $bug_table
WHERE team_id = '$t_team' && user_dept_name = '$name' && date_submitted BETWEEN '$t_start_string2' AND '$t_end_string2'";
$resultbug = db_query ($querybug);
$t_enquirys_total = 0;
$t_aenquirys = 0;
$t_complaint = 0;
$t_general = 0;
$t_request = 0;
$t_dailywork = 0;
$t_enguiry_count[$v_det_id[$i]] = 0;
$t_complaint_count[$v_det_id[$i]] = 0;
$t_general_count[$v_det_id[$i]] = 0;
$t_request_count[$v_det_id[$i]] = 0;
$t_dailywork_count[$v_det_id[$i]] = 0;
$t_aenquirys_total = 0;
$t_complaint_total = 0;
$t_general_total = 0;
$t_request_total = 0;
$t_dailywork_total = 0;
$t_aenquirys_total_perc = 0;
$t_complaint_total_perc = 0;
$t_general_total_perc = 0;
$t_request_total_perc = 0;
$t_dailywork_total_perc = 0;
$t_cat_total[$v_ud_id[$i]] = 0;
$t_all_cat_total = 0;
$t_enquiry_val = 'Enquiry';
$t_complaint_val = 'Complaint';
$t_general_val = 'General';
$t_request_val = 'Request';
$t_dailywork_val = 'Daily Work';
while ($rowbug = db_fetch_array ($resultbug)){
$t_enquirys_total++;
switch( $rowbug['category'] ) {
case $t_enquiry_val:
$t_aenquirys++;
$t_enguiry_count[$v_ud_id[$i]]++;
break;
case $t_complaint_val:
$t_complaint++;
$t_complaint_count[$v_ud_id[$i]]++;
break;
case $t_general_val:
$t_general++;
$t_general_count[$v_ud_id[$i]]++;
break;
case $t_request_val:
$t_request++;
$t_request_count[$v_ud_id[$i]]++;
break;
case $t_dailywork_val:
$t_dailywork++;
$t_dailywork_count[$v_ud_id[$i]]++;
break;
}
}
$t_cat_total[$v_ud_id[$i]] = $t_enguiry_count[$v_ud_id[$i]] + $t_complaint_count[$v_ud_id[$i]] + $t_general_count[$v_ud_id[$i]] + $t_request_count[$v_ud_id[$i]] + $t_dailywork_count[$v_ud_id[$i]];
$t_ud_total_perc[$v_ud_id[$i]] = number_format((($t_cat_total[$v_ud_id[$i]] / $t_enquirys_big_total) * 100), 2);
This is the half of the function that i use to retrieve data and also to calculate the percentage.When i try to use $querytotal,it indeed print out the data, but the second $querytotal below it does not show the data..please help
The thing happens because my execution time is 30sec. After add this particular code, it works fine.ini_set("max_execution_time", 0);