Pay Rate Calculator - php

I've looked online but all the $_POST and $_GET examples are for text not numbers.
This is not connecting to a database. It doesn't need form validation. It's just a single page project running locally on my system. I want to keep it as simple as possible.
The goal is to have an input where you can put in your hourly pay. For example 15.
Then when you press submit it posts back to the same page with a nice list of how much you make daily, weekly, monthly, and yearly.
Any help would be greatly appreciated.
<p>Input your Hourly Pay</p>
<form method="GET" action="" >
<input name="num1" type="text" >
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_GET['num1']) {
$rate = $_GET['num1'];
return $rate;
}
$result_day= $rate * 8;
$result_week = $rate * 8 * 5;
$result_month = $rate * 8 * 5 * 4;
$result_year = $rate * 8 * 5 * 4 * 12;
echo "Rate: " . $rate . "<br>";
echo "Daily: " . $result_day . "<br>";
echo "Weekly: " . $result_week . "<br>";
echo "Monthly: " . $result_month . "<br>";
echo "Yearly: " . $result_year . "<br>";
?>

I did some correction, but the calculation is still your original:
<?php
if (isset($_GET['num1'])) {
$rate = $_GET['num1'];
$result_day= $rate * 8;
$result_week = $result_day * 5;
$result_month = $result_week * 4;
$result_year = $result_month * 12;
echo "Rate: " . $rate . "<br>";
echo "Daily: " . $result_day . "<br>";
echo "Weekly: " . $result_week . "<br>";
echo "Monthly: " . $result_month . "<br>";
echo "Yearly: " . $result_year . "<br>";
}else{
echo "Please enter your rate!";
}

Use the onchange event with Javascript on any of the fields. You can then use Javascript to calculate the rest of the values and update those fields.
No PHP required. No time lag with a new page required. Users will love it.

Related

How to apply a discount to basket

I'm trying to apply a discount to my basket for each pair of 2 items that cost £1.50 each but I can't seem to get the hang of it. I draw the results from two SQL queries/tables of 'discount' and 'basket' Here's the following I've tried:
$discount_count[] = 0;
$i = 0;
while ($row2 = mysql_fetch_array($data_set2)) {
if ($row['product'] == $row2['product']) {
foreach ($row2 as $current_row) {
$actual_price = $row['product_cost'] * $row2['quantity'];
$discount_total = round(($actual_price - ($actual_price * ($row2['discount_percent'] / 100))), 2);
$total_price = $discount_total / $row2['quantity'] * $row['quantity'];
$product_total = $row['product_cost'] * $row['quantity'];
if ($i % 2 && $total_price * $row2['quantity'] > $row['product_cost'] * $row2['quantity']){
echo "<br /><br />actual price per 2: £" . $actual_price;
echo "<br />discount total: £" . $discount_total;
echo "<br />actual minus discount: £";
echo $actual_price - $discount_total;
echo "<br />total price: £" . $total_price;
echo "<br />product cost: £" . $product_total;
$discount_count[] = $actual_price - $discount_total;
}
$i++;
}
}
}

It always says Correct! What did I do wrong?

$number1 = mt_rand(1,9);
$number2 = mt_rand(1,9);
$total = $number1 * $number2;
echo "<form method='post'>";
echo $number1 . " x " . $number2 . " = <input type='number' name='num1' required /><br>";
echo "<input type='submit' value='submit!' name='done'>";
echo "</form>";
if (isset($_POST['done'])) {
if (isset($_POST['num1']) == $total) {
echo "Correct!";
} else {
echo "Wrong!";
}
}
It always says Correct! And I dont know why ( im a beginner ), I just want to check if num1 is equal to $total
You have many problems in your code :
you must first check if the post is submit then if its wrong type the form
if (isset($_POST['done'])) {
//your code
}
else {
//your form
}
save $total in session to reuse it when the form submit, in your case $total have different value every time
isset() return true or false you can't comparison true or false with integer value if you want to use isset your code must be like this:
if(isset($_POST['num1']) && $_POST['num1'] == $total) {
}
You're comparing isset($var) to $total. They're both truthy so the condition is always true as long as 'num1' is defined in your POST data.
Maybe you should do something like :
isset($_POST['num1']) && $_POST['num1'] == $total
You should also probably cast 'num1' to a number
As said by Tom Udding, every time you refresh the page it calls ALL of that code again, so number1 and number2 are being randomly selected again.
Your current code has no way of saving the previous variable values. An unconventional way would be to add a hidden form field with the answer to the question, like below:
<?php
if (isset($_POST['done']) && isset($_POST['num1']))
{
//Get answer from form.
$total = $_POST['answer'];
if ($_POST['num1'] == $total)
{
echo "Correct!";
}
else
{
echo "Wrong!";
}
}
$number1 = mt_rand(1, 9);
$number2 = mt_rand(1, 9);
$total = $number1 * $number2;
echo "<form method='post'>";
echo $number1 . " x " . $number2 . " = <input type='number' name='num1' required /><br>";
//Added hidden form with answer.
echo "<input type='number' hidden name='answer' value='$total' />";
echo "<input type='submit' value='submit!' name='done'>";
echo "</form>";
?>
HOWEVER...
In a realistic rich web application, you wouldn't put your answer in your form for users to see, this is where you can use sessions to track your user's information as they traverse (or in your case refresh) your page.
So a more practical answer to your question would be the following:
<?php
session_start();
if (isset($_POST['done']) && isset($_POST['num1']))
{
$answer = $_SESSION['answer'];
if ($_POST['num1'] == $answer)
{
echo "Correct!";
} else
{
echo "Wrong!";
}
}
$number1 = mt_rand(1, 9);
$number2 = mt_rand(1, 9);
$total = $number1 * $number2;
$_SESSION['answer'] = $total;
echo "<form method='post'>";
echo $number1 . " x " . $number2 . " = <input type='number' name='num1' required /><br>";
echo "<input type='number' hidden name='answer' value='$total' />";
echo "<input type='submit' value='submit!' name='done'>";
echo "</form>";
?>

codeigniter substract two variables from two different loop

hello guys i need to find the differences between two different loops which is summation loop and totals loop as shown in controller code below
function summary() {
echo '<table>';
$summation=$this->select_model->sum_income("date_of_income BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($summation as $sum){
echo '<tr><td>Total Income</td> <td >'.$sum['total'].'</td></tr>';
}
$totals=$this->select_model->sum_expenditure("date_of_expenditure BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($totals as $total){
echo '<tr><td>Total Expenditure</td><td ><strong>'.$total['total'].'<</td></tr>';
}
//display differences here:
echo '<tr><td>'.**$sum['total']-$total['total']**.'</td></tr>'
echo '</table>';
}
You would have to create the variables outside of the loops and add (or subtract) as the loop progresses.
$sum = 0;
$total = 0;
foreach... {
$sum += $sum;
}
foreach... {
$total += $total;
}
echo $sum - $total;
If you're adding negative numbers, the math will work without having to do anything specific.

Retain the value of a random generated number

I am learning php atm and i decided to make a simple game but now im confronted with a problem. I have the following code:
<form action="wolf.php" method="POST">
<input type="submit" value="Attack" name="submit"/> <br />
<?php
$hp = 100;
if(isset($_POST['submit'])) {
$attack = $_POST['submit'];
$damage = mt_rand(5, 30);
$newhp = $hp - $damage;
if ($attkdamage = $hp - $damage ) {
echo "Your HP is: ". $newhp . "<br / >";
echo "You took: " . $damage . " damage!";
}
}
?>
As you can see I have a variable with an integer (100) and a simple mt rand. What I want is that after I submit and get a $newhp (100 - the random number), that number to replace $hp. and the next time I submit the button I want the value of $damage to be subtracted from the previous action, so basically to save the $newhp as $hp.
If possible storing this value in a session variable that will be saved in the browser you use, for more information read here $_SESSION
<?php
session_start();
if(!isset($_SESSION["hp"]))
$_SESSION["hp"]=100;
if(isset($_POST['submit'])) {
$attack = $_POST['submit'];
$damage = mt_rand(5, 30);
$newhp = $_SESSION["hp"] - $damage;
if ($attkdamage = $_SESSION["hp"] - $damage ) {
echo "Your HP is: ". $newhp . "<br / >";
echo "You took: " . $damage . " damage!";
}
echo "<pre>previus hp " .$_SESSION["hp"];
$_SESSION["hp"]= $newhp;
echo "next hp " . $_SESSION["hp"]."</pre>";
}
?>

My php code not getting add correct

I making one counter, I am using PHP and almost everything was going fine but i found one small bug in it.
My first two step on counter is going fine that is $bid and $stax.
My last result $pay should be : 8138 + 814 + 448 = 9400 but it is giving me $9,399
My Output is:
Value $8,138 bid $814 tax $448 You Pay $9,399
Value $8,952 bid $895 tax $492 You Pay $10,339
Value $9,847 bid $985 tax $542 You Pay $11,373
Here is my php
<?php
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = $v / 10; // output : $814
$ftax = $v + $bid;
$stax = $ftax / 20; // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = $v * 1.1;
} while ($i <= 2);
?>
Thanks in advance :)
Change your echo line to not drop decimals:
echo "Value $" . $v . " bid $" . $bid . " tax $" . $stax . " You Pay $" . $pay . "<br />";
Value $8138 bid $813.8 tax $447.59 You Pay $9399.39Value $8951.8 bid $895.18 tax $492.349 You Pay $10339.329Value $9846.98 bid $984.698 tax $541.5839 You Pay $11373.2619
So 8138 + 813.8 + 447.59 = 9399.39, which rounds down to 9399.
On the other hand, if you round immediately, 813.8 rounds up to 814 and 447.59 rounds up to 448, but you've just added 0.61 to your calculation before even starting, which obviously results in a higher number (9400).
This is why math and science teachers tell you not to round until the very end, since each time you do it, your answer gets less accurate.
if you dont want to use decimals, round off all division calculations by using round(). Also, multiplications with decimals. See it in action here
$i = 0;
$v = 8138; // value : 8138
do {
$i++;
$bid = round($v / 10); // output : $814
$ftax = $v + $bid;
$stax = round($ftax / 20); // output : tax $448
$pay = $v + $bid + $stax; // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = round($v * 1.1);
Lack of casting/decimals be ye problem matey. har har har

Categories