I have parsed over the variable $museum from another page under the name 'm'. If a check box was ticked on the page I parsed it from then on this new receipt page it will add 5 to the cost, or add 0. Cost is then echoed. But either way it is returning 0. First time poster and PHP beginner so sorry if I got something wrong or was not specific enough. Here is the code from the receipt page.
<?php
$cost = 0;
if(isset($_GET['m']))
{
$museum = $_GET['m'];
if($museum==false){
$cost + 0;
}else{
$cost + 5;
}
}
echo $cost;
?>
You are not using the return value of the operation. Try
$cost += 5;
instead of
$cost + 5;
Try this:
if($museum==false){
$cost = $cost + 0;
}else{
$cost = $cost + 5;
}
Related
Im learning PHP now and was given a task which I thought I could follow through, but I get an
'int(459)'
printed out on the website.
Here is the task and my attempt to solve it:
Multiply your age by the numbers of yours
you went to school and put it isnide of
variable named total.
Then minus the total by 3.
Then check, if total is greater or equal
to 12 and put the result inside of another variable.
Then use var_dump to see if its true, or false.
<?php
$age = 33;
$schoolyears = 14;
$total = $age * $schoolyears;
$total -= 3;
$total >= 12;
$newVar = $total;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
var_dump($newVar);
?>
</body>
</html>
Appreciate your answers!
Rob
UPDATE!
After editing it lloks like this and it works.
<?php
$age = 33;
$schoolyears = 14;
$total = $age * $schoolyears;
$total -= 3;
$total = $total >= 12;
$newVar = $total;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
var_dump($newVar);
?>
</body>
</html>
It puts out :
bool(true)
This line doesn't do anything:
$total >= 12;
It produces a value, but you don't store that value anywhere. On the next line you just copy the value from $total (which is 459) to a new variable:
$newVar = $total;
It looks like you meant to combine these into this instead:
$newVar = $total >= 12;
In general, it looks like you're confusing these operators:
-=
>=
While they share the same second character, they are not related in any way. The first one subtracts the second value from the first and assigns it back to the first variable, kind of a double operation and a shorthand for:
$var1 = $var1 - $var2;
But the second one does no assigning. It literally semantically means "greater than or equal to". It performs a comparison between two values, but doesn't modify anything.
When you state $total >= 12; it makes the check but does nothing with the information.
You need to set $newVar = $total >= 12.
From your question, step by step;
$age = 33;
$schoolyears = 14;
$negative = 3;
$total = $age * $schoolyears - $negative;
if ($total >= 12) {$result = $total; $status = true;} else {$status = false;}
var_dump($status);
I got it. This has to be....
$total >= 12;
like this:
$total = $total >= 12;
Thanks, I leave this up for other people looking foir similar questions.
I wanted to know how I present a percentage, here is the code
//The number taken from the database
$minos = $ud['bnk']['gold'];
The number that is supposed to be in percent through the database (the database has a number and not a percentage for example 2)
$plus= $ud['bnk']['ent_level'];
And here is a simple calculation of X + 2% = Y
$sava = $minos + $plus;
I tried to do this, according to an internet guide, but it doesn't work for me, I want the number to be a percentage and not successful
function get_percentage($total, $number)
{
if ( $total > 0 ) {
return round($number / ($total / 100),$ud['bnk']['ent_level']);
} else {
return 0;
}
}
$minos = $ud['bnk']['gold'];
$plus = get_percentage(100,$ud['bnk']['ent_level']).'%';
$sava = $minos + $plus;
I solved the equation with what you gave me, thank you very much, the way is:
$minos = $ud['bnk']['gold'];
$plus = ($minos / 100) * $ud['bnk']['ent_level'];
$sava = $plus;
Thanks so much to everyone who helped
I'm trying to add some numbers in a foreach loop in my PHP code. I am getting a percentage of numbers in a while loop in my MySQL query for each result that I get in my PHP page.
All I need to do is to add up the final values in and show them as total.
This is how I make up the percentage in my while loop in my MySQL query:
$percentage = 10;
$totalWidth = $fees;
$new_width = ($percentage / 100) * $totalWidth;
The $fees value is dynamic and it is different for each result in my while loop. the code above works as it should.
Now I want to add up all the values of $new_width. For example:
If one result's $new_width is 25 and the other one is 10 and another one is 5, I need to do this: $total = 25 + 10 + 5;
So I tried something like this:
$total = 0;
foreach($new_width as $var) {
$total = $var + $var;
}
echo $total;
but the above code doesn't really make sense and it won't do anything at all.
Could someone please advise on this matter?
First you want to change this line in your while loop so you get an array:
$new_width = ($percentage / 100) * $totalWidth;
to this:
//Declare it as an array before your while loop
$new_width = array();
//In your while loop
$new_width[] = ($percentage / 100) * $totalWidth;
//^^ See here
After this you just have to change the line in your foreach loop like this:
$total = $var + $var;
to this:
$total += $var;
(If you want you also can do this in your while loop)
If you have an array of numbers and you want to calculate the sum of those numbers, you should use array_sum().
According to the logic, you are setting the total to 2 X $var.
My answer is very similar, but you add it to the total which is outside of the loop and the value will keep growing:
$total = 0;
foreach($new_width as $var) {
$total += $var;
}
echo $total;
Or simply as stated before, if it is the only value in the array:
$total = array_sum($new_width);
So this will be very specific question, but I am losing my sanity as I was staring at this for the better part of the day. So here I wrote this function to calculate the price dependent commission:
function commision($amount, $commision_arg = 0){
// $temp_amount = 0;
// $commision = 0;
if($amount > 999){
$temp_amount = $amount - 999;
$commision_arg += $temp_amount/100*1;
return commision(999, $commision_arg);
}else if($amount>249&&$amount<=999){
$temp_amount = $amount - 249;
$commision_arg += $temp_amount/100*3;
return commision(249, $commision_arg);
}else if($amount>49 && $amount<=249){
$temp_amount = $amount - 49;
$commision_arg += $temp_amount/100*5;
return commision(49, $commision_arg);
}else if($amount <= 49){
return number_format($commision_arg + 3.5, 2);
}
}
Basically if $amount is less then 49 then it is just flat 3.5 rate, if $amount is 49 to 249 then it is flat rate 3.5 + 5% of anything in 49-249 range.
I am not sure if this is good way to do this or not, but it works. However there is issue with another method that uses this function:
public function amountPayable(){
$amount = $this->uri->segment(3);
echo $amount - commision($amount);
}
This is just a function in codeigniter controller, that is called by AJAX request, and it should be returning the amount payable by the customer, which is then injected into DOM.
And this is where the problem lies.
Basically if I pass the $amount that equal to 100.000, what I get back from amountPayable is 99.999, whereas it should be: 98973.9. if I change echo $amount - commision($amount); to echo commision($amount); then I get back 1026.1. So the commision() function returns the correct number. But for some reason $amount - commision($amount); (where $amount = 100000) evaluates to 1.
How can this be?
I have a requirement where users are forced to choose the multiple of (n) quantity of a product.
The (n) value is set with each product that can be any number.
customer can only purchase the quantity of product in the multiple of (n) quantity set with product.
Suppose if (n) is 5 and user entered quantity as 4 and says Add to Cart. I have to add quantity of that product as 5 automatically.
and if user entered 6 as quantity then I have to add the 10 quantity of that product.
How I go about that?
I am not getting what logic should be applied here.
$entered_quantity = 6;
$suppose_n = 5;
$quantity = ceil($entered_quantity / $suppose_n) * $suppose_n;
echo $quantity;
prints 10
that's not php specific;
what you wonna do is to compute.
ceiling(q / n) * n
where q is the user's quantity,
n is the multiplicity
You could try getting the remainder of the number when dividing by the given n
e.g.:
$n = 5;
$amount = 6; // This would be the input, so replace the 6 with a $_POST/$_GET/etc.
$batches = floor($amount / $n);
$rest = $amount % $n;
if ($rest > 0) {
$batches += 1;
// You could give the user feedback here that they didn't put in a full multiple of $n
}
// $batches now contains the right amount of batches, so to get the total:
$total = $batches * $n;
Ofcourse this can be condensed a lot, but this might give a better overview of what happens :).
Try the below function.
function getNextMultipleOfFive($n) {
$tmp=explode('.',($n/5));
if($tmp[1]) {
return ($tmp[0]+1)*5;
}
return $tmp[0]*5;
}
With a do...while loop:
$q = 6; // quantity by user input
$n = 5; // per purchace amount
$i = 0;
if ($q > 0)
{
do
{
$i += $n;
}
while ($i <= $q);
}
echo $i; // 10
Note: not very effective if $q >> $n