PHP array, table, form, multiplying... argh - php

I have tried for hours, and hope you can help me now :
I am trying to get values from a HTML form via $_POST into a table via PHP, and multiplying two numbers: ['amount'] * ['price'] = $totalprice
So far so good.
I get something that might look like this:
Description – Amount – Format – Price – Total ($totalprice)
Spoon – 3 – pieces – 4 – 12
Plate – 2 – pieces – 3 – 6
Glass – 6 – pieces – 3 – 18
Now how do I sum up all the “Total” ($totalprice)’s 12+6+18 so I get the result: 36 ?
The piece of code I’m struggling with:
foreach($_POST['description'] as $value)
{
echo "<tr><td>";
echo $i+1;
echo "</td>
<td>".$value."</td>
<td><center>".$_POST['amount'][$i]."</td>
<td><center>".$_POST['format'][$i]."</td>
<td><center>".$_POST['price'][$i]."</td>";
//Figures out the total price = amount * price
$x1 = $_POST['amount'] [$i];
$x2 = $_POST['price'] [$i];
echo "<td><center>";
$totalprice = $x1 * $x2;
echo $totalprice;
//Figures out the total price = amount * price
$i++;
}

You just need to sum all totals while iterating
$grand_total = 0;
foreach($_POST['description'] as $value)
{
echo "<tr><td>";
// code here...
$totalprice = $x1 * $x2;
echo $totalprice;
$grand_total = $grand_total + $totalprice;
$i++;
}
echo $grand_total;

use this code
$totalprice = 0;
foreach($_POST['description'] as $value)
{
echo "<tr><td>";
echo $i+1;
echo "</td>
<td>".$value."</td>
<td><center>".$_POST['amount'][$i]."</td>
<td><center>".$_POST['format'][$i]."</td>
<td><center>".$_POST['price'][$i]."</td>";
//Figures out the total price = amount * price
$x1 = $_POST['amount'] [$i];
$x2 = $_POST['price'] [$i];
echo "<td><center>";
$totalprice = $totalprice + ($x1 * $x2);
$i++;
}
echo $totalprice;

Related

PHP Add numbers 1-10 and only even numbers 1-10. Display sums separately with one loop

I'm trying to use ONE PHP for loop to echo the sum of the numbers 1-10 as well as echo the sum of only the even numbers. I seem to have a problem as these iterations won't be "parallel"
Code:
<?php
$sum = 0; $evensum = 0;
for($x = 1, $y=2; $x<=10, $y<=6; $x++, $y += 2) {
$sum = $sum + $x; $evensum = $evensum + $y;
}
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
total sum should reflect 55 (1+2+3+4+5+6+7+8+9+10) and even sum should reflect 30 (2+4+6+8+10)
Just Use
<?php
$sum = 0; $evensum = 0;
for($x = 1; $x<=10; $x++) {
// sum all the number
$sum = $sum + $x;
// check the number is even
if( $x % 2 === 0 ) {
// sum only the even numbers
$evensum = $evensum + $x;
}
}
// output
echo "total sum= ". $sum, ", even sum=" . $evensum;
?>
Another approach is using range() and array_functions
$arr=range(1,10);
echo $sum=array_sum($arr);
function even($var)
{
return(!($var & 1));
}
echo $even=array_sum(array_filter($arr, "even"));
Sum all ranges
$all_sum=array_sum(range(1,10));
Sum even number (0,2,4,6,8,10)
$even_sum = array_sum(range(0,10,2));

Loop to Calculate Price

trying to create a loop to calculate a price but cant get my head around how to do it.
$sell = ($price*$vat);
$profit = ((($price*$vat)*0.04)+0.2);
function newSell($price1){
$price1 = $price1 + 0.10;
return $price;
}
do {
$price1 = newSell($price);
$profit = ((($price1*$vat)*0.04)+0.2);
} while ($profit < 0);
$price is the price of my item
$sell is my starting price.
$profit is the calcualtion to work out my profit.
What i want to do is look around and if my profit is less than 0 i want to add 10p (0.10) to my price and then recalculate my profit and evaluate it again. Want to keep going until my profit is above 1 at which point it stops and my new selling price has been set.
Cant for the life of me get my head around this!
Many thanks
First of all your newSell function should look like this:
function newSell($price) {
return $price + .1
}
It currently does nothing.
Second, are you sure this is the way you should be trying to solve this? It seems to me like you are just trying to solve for $profit >= 1. It's a simple equation that takes no loops whatsoever.
Apparently $profit = $price * $vat * .04 + .2, so you are really looking at 1 <= $price * $vat * .04 + .2 or 20 / $vat <= $price
So if $price >= 20 / $vat then $profit >= 1.
To do specifically what you asked:
do {
$profit = $price * $vat * .04 + .2;
$price = $price + .1;
} while ($profit < 1);

If-else statement to count some variable

I need some help to solved this algorithm problem with How a Person is popular in his city.
My situation
How the algorithm should work like
If a person "mark" has 500 friends in his city out of 500,000.
(500/500,000)*50,000 = 5
So 5 in 50,000 people Know him right.
But When friends count increase the 50,000 should decrease
If "sam" has 1000 friends then
(1000/500,000)*25000 = 5
So 5 in 25000 people know his name
Yes we could implement this in if/else condition
If so then i have to write 500 lines of code.
Is there another way to do this in PHP?
<?php
$totalfriends = 100;
$totali = 5000000;
$name = "Sam";
if ($totalfriends >= 100 && $totalfriends <= 499 ) {
$r = ($totalfriends/$totali)*50000;
echo round($r),' ',"in 50K People on City regonize this Profile";
}else if ($totalfriends >= 500 && $totalfriends <= 999) {
$r = ($totalfriends/$totali)*25000;
echo round($r),' ',"in 25K People on City know".$name;
}else{
echo "";
}
?>
is this what you are looking for?
foreach([100, 500, 543, 1000, 5000, 51000, 500000] as $my_friends)
echo '5 in '. getScoreOf($my_friends) . "<br>";
function getScoreOf($my_friends){
$of = 5;
$total = 5e5; //that's 500,000 ;)
$step = 100; //minimum step, so output is not "4604" but "4600"
$out_of = $total / $my_friends * $of;
return $out_of > $step? round($out_of / $step) * $step: round($out_of);
}
run it in sandbox
edit: solution merged with original code
<?php
$of = 5;
$totalfriends = 100;
$name = "Sam";
echo $of ." in ". getScoreOf($of, $totalfriends) ." people in city know ". $name;
function getScoreOf($of, $my_friends){
$total = 5e6; //that's 5,000,000 ;)
$step = 100; //minimum step, so output is not "4604" but "4600"
$out_of = $total / $my_friends * $of;
return $out_of > $step? round($out_of / $step) * $step: round($out_of);
}

Calculate how many times a static number fits into another number

For a surface calculation I am searching for the following solution.
I have a size of the surface like 60m², for this square I have 2 kind of
materials sizes. Material size of 2m² and 4m². The challenge for me now is
to calculate the needed materials efficiently as possible and keep a rest
of the material to the minimum.
So, filling a surface of 60m² with most as possible with 4m² materials and fill it up with 2m2 to keep material to the minimum.
It's simple. With this method, you can use any number and any size of materials.
Store your materials into an array. Loop through on that array, make the calculations, and store the "rest" in another variable. If at the end there will be some rest, then add 1 more from the last item.
$materials = array(2,4,8);
$surface = 63;
rsort($materials);
$rest = $surface;
$isFinished = false;
$data = array();
foreach ($materials as $material) {
$result = $rest / $material;
if ($result >= 1) {
$data[$material] = floor($result);
$rest -= $material * floor($result);
}
}
if ($rest > 0) {
$data[end($materials)]++;
}
echo "For a " . $surface . " you need the following materials: <br />";
foreach ($data as $key => $val) {
echo "Material " . $key . " * " . $val ."<br />";
}
Output is:
For a 63 you need the following materials:
Material 8 * 7
Material 4 * 1
Material 2 * 2
assuming you are using PHP
This will be a start to find the material with minimum rest.
function getMinRest($surface, $num1, $num2){
$rest1 = $surface % $num1;
$rest2 = $surface % $num2;
return $rest2 <= $rest1 ? $num2:$num1;
}

Change for in PHP with table

<table><tr>
<?php
for($i=0;$i<15;$i++) {
if($i%5 == 0) {echo '</tr> <tr>';}
?><td><?php echo $i ?></td>
<?php
}?>
</tr>
</table>
this generate:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
how can I make:
0 3 6 9 12
1 4 7 10 13
2 5 8 11 14
?
You need a nested loop
<table>
<?php
$rows = 3;
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < 5 ; $j++ ) {
echo "<td>" . ($j * $rows + $i) . "</td>";
}
echo "</tr>";
}
?>
</table>
I tried to make the variable names descriptive:
<table>
<?php
// Set the number of rows, cols, and starting number here:
$number_rows = 3;
$number_cols = 5;
$starting_num = 0;
// You can use foreach w arrays... much easier
$rows = range(0,$number_rows - 1);
$cols = range(0,$number_cols - 1);
foreach($rows as $one_row) {
?>
<tr>
<?php
foreach($cols as $one_col) {
// Do the calculation
echo "<td>" .
($starting_num + $one_col + ( max($rows) * $one_col ) + $one_row) .
"</td>";
}
?>
</tr>
<?php
}
?>
</table>
Working example like in the OP.
Now let's say you want to go from (1300-1431), then you start at 1300 and want a 4 x 8.
$number_rows = 8;
$number_cols = 4;
$starting_num = 1300;
Like this.
There are two "tricks."
The first is using range() to quickly define an array integers. I find arrays more pleasant to work with than raw numbers, since arrays can be worked on with the intuitive construct of foreach().
The second is figuring out how to know what number to print if you have the column, row, and starting, number. It's simplest to figure out the numbers for the first row and go from there. Let's number the rows and cols from 0. So col:0 row:0 will be the starting number. Col:1 Row:0, the number to the right, will just be the starting number + one (the column number) + the number of rows less one (easiest to see by looking at the matrix of number), and the number of rows less one is the maximum number in the rows array. So for the first row we have:
$starting_num + $one_col + ( max($rows) * $one_col )
then all we just add the row number to take that into account, and we've got it all:
$starting_num + $one_col + ( max($rows) * $one_col ) + $one_row
Tested example with given start and end:
<table>
<?php
$start = 1300;
$end = 1432;
$n = $end - $start + 1;
$cols = 5;
$rows = ceil($n / $cols);
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < $cols ; $j++ ) {
$val = $j * $rows + $i;
echo "<td>";
echo ($val < $n) ? $val + $start : ' ';
echo "</td>";
}
echo "</tr>";
}
?>
</table>

Categories