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
Related
i like to sum all row values from the $orderfeed_query. But when i echo the $sum, i get just the sum from the last loop.
how can can i add the sum of all following loop on the $sum variable? I dont know how many loops a order have.
$checkorder = mysql_query("SELECT * FROM orders WHERE `email` = '$email' ") or die(mysql_error());
while ($row = mysql_fetch_assoc($checkorder)) {
$orderid = $row["orderid"];
$check_order = $row["check_order"];
if($check_order[0] == 1){
$orderfeed_query = mysql_query("SELECT * FROM orderfeed WHERE `orderid` = '$orderid' AND `product` = '1'") or die(mysql_error());
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = $row["signaturewiz"];
$flurstueckwiz = $row["flurstueckwiz"];
$uploadwiz = $row["uploadwiz"];
$exsignaturewiz = $row["exsignaturewiz"];
$ibanwiz = $row["ibanwiz"];
$sum = $signaturewiz+$flurstueckwiz+$uploadwiz+$exsignaturewiz+$ibanwiz;
echo $sum;
}
}
}
}
This code
$sum = $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;
overwrites sum each time. You must add to sum not to overwrite
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;
And declare $sum = 0; before main loop
Several people have pointed out that $sum should be outside the loop, and that is indeed correct. However, MySQL can do this all for you:
$orderfeed_query = mysql_query("SELECT SUM(signaturewiz + flurstueckwiz + uploadwiz + exsignaturewiz + ibanwiz) FROM orderfeed WHERE orderid = '$orderid' AND product = '1'") or die(mysql_error());
if ($row = mysql_fetch_row($orderfeed_query)) {
echo $row[0];
}
Move the echo of $sum outside the loop
Use += rather than + to accumulate a total over multiple iterations
Initialize your variable before using += as if $sum has an undefined value it can mess up the count when using +=
$sum = 0; // init variable
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = $row["signaturewiz"];
$flurstueckwiz = $row["flurstueckwiz"];
$uploadwiz = $row["uploadwiz"];
$exsignaturewiz = $row["exsignaturewiz"];
$ibanwiz = $row["ibanwiz"];
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz +
$exsignaturewiz + $ibanwiz;
}
echo $sum;
ADDITIONAL INFO:
You see 12 and not 3 so the data from your table I assume is text and not numeric so do this to convert text numbers to integers
$sum = 0; // init variable
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = (int)$row["signaturewiz"];
$flurstueckwiz = (int)$row["flurstueckwiz"];
$uploadwiz = (int)$row["uploadwiz"];
$exsignaturewiz = (int)$row["exsignaturewiz"];
$ibanwiz = (int)$row["ibanwiz"];
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz +
$exsignaturewiz + $ibanwiz;
}
echo $sum;
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;
Ok, I don't even know where to start with this one! I'll try and explain what I want to achieve, and we'll go from there....
I have a list of dates each with an associated number, say from 20-100. What I want to do is to output the date in a shade which represents the associated number. So 20 would display in a light blue and 100 in a dark blue. My code so far looks like this...
dateArray = Array('2001-01-01'=>30, '2001-02-01'=>40, '2001-03-01'=>50, '2001-04-01'=>60, '2001-05-01'=>70, '2001-06-01'=>80, '2001-07-01'=>90, '2001-08-01'=>90, '2001-09-01'=>80, '2001-10-01'=>70, '2001-11-01'=>60, '2001-12-01'=>50)
$maxNum = max($dateArray);
$minNum = min($dateArray);
foreach($dateArray AS $date => $num){
$lightest = 'rgb(204,204,255)';
$darkest = 'rgb(0, 0, 179)';
///magic that converts $num into $shade goes here///
echo "<span style='color:$shade'>$date</span><br>"
}
Any ideas? Thanks
I would do something like that :
$dateArray = Array('2001-01-01'=>30, '2001-02-01'=>40, '2001-03-01'=>50, '2001-04-01'=>60, '2001-05-01'=>70, '2001-06-01'=>80, '2001-07-01'=>90, '2001-08-01'=>90, '2001-09-01'=>80, '2001-10-01'=>70, '2001-11-01'=>60, '2001-12-01'=>50)
// get max and min values
$maxNum = max($dateArray);
$minNum = min($dateArray);
// set rgb values for max and min
$lightest = array(204, 204, 255);
$darkest = array(0, 0, 179);
foreach($dateArray AS $date => $num)
{
// get a "delta" where the current num value is
$delta = ($num / $maxNum) - $minNum;
// get a pro-rata values thanks to $delta
$shadesNum = array(
$delta * ($lightest[0] - $darkest[0]) + $darkest[0],
$delta * ($lightest[1] - $darkest[1]) + $darkest[1],
$delta * ($lightest[2] - $darkest[2]) + $darkest[2]
);
echo "<span style='rgb(".implode(',', $shadesNum).")'>$date</span><br>";
}
Some languages have a "lerp" function - linear interpolation. Quite useful.
My suggestion:
for ($x1=20; $x1<=100; $x1+=10)
echo $x1 . ": " . getshade($x1) . "<br />\n";
function getshade($num) {
$rlight = 204;
$glight = 204;
$blight = 255;
$rdark = 0;
$gdark = 0;
$bdark = 179;
$lightnum = 20;
$darknum = 100;
$k01 = ($num-$lightnum)/($darknum-$lightnum); // 0 to 1
$rshade = ilerp($rlight, $rdark, $k01);
$gshade = ilerp($glight, $gdark, $k01);
$bshade = ilerp($blight, $bdark, $k01);
return "rgb($rshade,$gshade,$bshade)"; }
function lerp($start, $end, $k01) { // linear interpolation
return $k01*$end + (1.0-$k01)*$start; }
function ilerp($start, $end, $k01) { // integer lerp
return intval($k01*$end + (1.0-$k01)*$start); }
EDIT: Same thing but better:
$rgblight = [204,204,255];
$rgbdark = [0,0,179];
$numlight = 20;
$numdark = 100;
for ($x1=20; $x1<=100; $x1+=10)
echo $x1 . ": " . getshade2($x1, $numlight, $numdark, $rgblight, $rgbdark) . "<br />\n";
function getshade2($num, $numlight, $numdark, $rgblight, $rgbdark) {
$k01 = ($num-$numlight)/($numdark-$numlight);
for ($i1=0; $i1<3; $i1+=1)
$rgb[$i1] = ilerp($rgblight[$i1], $rgbdark[$i1], $k01);
return "rgb({$rgb[0]},{$rgb[1]},{$rgb[2]})"; }
if condition matches but else if condition is not working when i insert $total_salary = 10000 and $salary_type = NO.
if ($this->request->is('post')) {
$DATA = $this->data;
$employee_salary = $this->Salary->save($DATA);
$total_salary = $employee_salary['Salary']['salary'];
$salary_type = $employee_salary['Salary']['salary_in_ctc'];
echo $total_salary . $salary_type;
if (($total_salary > 15000) && ($salary_type === 'YES')) {
$pf_company = 1500;
$pf_employee = 1500;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
} elseif (($total_salary < 15000) && ($salary_type === 'NO')) {
echo'hello';
$pf_company = 1200;
$pf_employee = 1200;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
}
try to match the uppercase condition and also check for white space in you conditions.
strtoupper(trim($salary_type))
The possible second issue could be:
As you are checking the datatype also in the condition $salary_type ==='NO' and in the post you stated you are passing $salary_type = NO. which doesn't seems to be a string type.
Please try removing one = from condition or passing NO as a string to validate the condition
I have a function that should calculate prices based on quantity.
The function should loop through every order and calculate every product price based on quantity, then should return order total price.
What i'm doing wrong?
public function getSumaComanda($cos) {
$suma = $this->_db->query(sprintf("SELECT (#pretredus:=`pretredus`) AS `pretredus`,(CASE #pretredus WHEN 0 THEN `prettotal` ELSE `pretredus` END) AS `prettotal` , cantitate FROM comenzi WHERE cos = '%d'", $cos));
$suma->execute();
$data_array = $suma->fetchAll(PDO::FETCH_ASSOC);
$count = $this->_db->query(sprintf("SELECT COUNT(*) FROM cosuri WHERE id='%d'", $cos));
$num = $count->fetchColumn();
for ($x = 0; $x < $num; $x++) {
$price = $data_array['cantitate'][$x] * $data_array['prettotal'][$x];
$pret = $pret + $price;
$pret = number_format($pret, 2, ".", "");
}
$rez = $pret;
return $rez . ' Lei';
}
You should learn how to basically debug your variables. With using var_dump($data_array); you can see, what's in there.
You have to use the numerical index first:
$price = $data_array[$x]['cantitate'] * $data_array[$x]['prettotal'];
Nevertheless, your second query is useless. You don't have to count the results and can use instead a while-loop:
public function getSumaComanda($cos) {
$suma = $this->_db->query(sprintf("SELECT (#pretredus:=`pretredus`) AS `pretredus`,(CASE #pretredus WHEN 0 THEN `prettotal` ELSE `pretredus` END) AS `prettotal` , cantitate FROM comenzi WHERE cos = '%d'", $cos));
$suma->execute();
while ($data_array = $suma->fetch(PDO::FETCH_ASSOC)) {
$pret += $data_array['cantitate'] * $data_array['prettotal'];
}
return number_format($pret, 2, ".", "") . ' Lei';
}