How can I subtract a value gotten from the loop - php

This is my code
// $res[] has two values(75000, 30000)
// $_SESSION['requestedAmt'] = 35000
while ($row = $queryWithdrawalTable->fetch_assoc()) {
$res[] = $row['profit_amt'];
foreach ($res as $amt) {
if ($_SESSION['requestedAmt'] > $amt) {
$calc = $_SESSION['requestedAmt'] - $amt;
} else {
$calc = $amt - $_SESSION['requestedAmt'];
}
}
echo $calc; // outputs 5000 40000
}
What I actually want to achieve is
$a[] = (1500, 3000);
$b = 2500;
while(fetch the data) {
$res = $a - $b;
}
// output = 2000
I want the value in $b to subtract the first value in $a(1500) and use result gotten from the first calculation to subtract from the second value(3000). please note I'm new to programming so any suggestion will be accepted thanks guys.
I want to be able to assign a single user to pay multiple users. for example user1 has to pay $100 to user2($25), user3($50) and user4($25). I have user1 money in $_SESSION['requestedAmt'] while the other users in $row['profit_amt']

You are almost there, you only perform the calculation after you have all the value inside the $res array
while ($row = $queryWithdrawalTable->fetch_assoc()) {
$res[] = $row['profit_amt'];
}
$calc = $_SESSION['requestedAmt'];
foreach ($res as $amt) {
if ($_SESSION['requestedAmt'] > $amt) {
$calc = $calc - $amt;
} else {
$calc = $amt - $calc;
}
}
echo $calc;
Or you don't need the $res array at all, perform the calculation while fetching the rows:
$cals = $_SESSION['requestedAmt'];
while ($row = $queryWithdrawalTable->fetch_assoc()) {
if ($_SESSION['requestedAmt'] > $row['profit_amt']) {
$calc = $calc - $amt;
} else {
$calc = $amt - $calc;
}
}

Related

How to remove elements of an array from an array with equal interval

I am working on taxi app, after ride completes i have to draw map for which i use google map apis. let suppose my way-points are
array($coordinates1,$coordinates2,$coordinates3,$coordinates4,$coordinates5,$coordinates6,.........,$coordinates40)
the problem is that google map api accept only maximum of 23 way-points, for this purpose I have to skip many coordinates to pass, here my idea is to unset elements of array with equal intervals so that the route remain same. Please someone guide me how to remove many number of elements from an array with equal interval, just like i need 23 and total elements are 40 so that i need like
array($coordinates1,$coordinates4,$coordinates7,$coordinates9,$coordinates13,$coordinates16,.........,$coordinates39)
I have found a possible way, its a little bit lengthy but i got exact required result
$required_elements = 23;
$way = array();
for($ini=1;$ini<=78;$ini++)
{
$way[] = "Co-Ordinated:".$ini;
}
$total_elements = count($way);
list($way1, $way2) = array_chunk($way, ceil(count($way) / 2));
$differrence_element = $total_elements-$required_elements;
$skip_element = ceil($total_elements/$differrence_element);
$output = array();
if($total_elements > $required_elements)
{
$i=1;$j=0;
foreach($way as $x)
{
if($i == $skip_element)
{
$i=1;$j++;
continue;
}
else
{
$output[] = $way1[$j];
if(count($output) >= $required_elements) break;
$output[] = $way2[$j];
if(count($output) >= $required_elements) break;
$i++;$j++;
}
}
}
else
{
$output = $way;
}
$output1 = array();
$output2 = array();
$i=0;
foreach($output as $g)
{
if($i%2 == 0)
$output1[] = $g;
else
$output2[] = $g;
$i++;
}
$result = array_merge($output1,$output2);
print_r($way);
echo "<br><br><pre>";
print_r($result);
echo '</pre>';

Range values to be calculated in PHP Loop

I need to calculate if avg is > 7.75 means Heavy else No.
What the concern is that.
Need to calculate that from $x to $y range of all numbers comes int.
But i was able to get one Avg only not sure what to do to get all numbers Average values and store in Array comes in that range.
function heavyDecC ($x,$y)
{
for($i=$x;$i<=$y;$i++)
{
$numb = $i;
$numleng = strlen((string)$i);
$int = 0;
for($j=0;$j<$numleng;$j++)
{
$int = $int + ($numb % 10);
$numb = (int)$numb / 10;
}
$avg = 0.00;
$avg = round($int/$numleng,2);
if($avg>7)
{
$isHeavy = "Yes";
}
else
{
$isHeavy = "No";
}
return $result[] = array($i,$avg,$isHeavy);
}
}
Here is a simple solution
<?php
$results = heavyDecC(2685, 5875);
// Display results like this
foreach ($results as $id){
echo "--------------------------<br/>";
foreach($id as $key => $val){
echo $key . " - " . $val . "<br />";
}
}
function heavyDecC ($x,$y) {
for($i=$x; $i<=$y; $i++){
$num = $x;
$isHeavy = "No";
$num_length = strlen((string)$num);
$array = array_map('intval', str_split($num));
$sum = array_sum($array);
$average = ($sum / $num_length);
if($average > 7){
$isHeavy = "Yes";
}else {
$isHeavy = "No";
}
$newdata = array (
'Number' => $num,
'average' => $average,
'is_heavy' => $isHeavy
);
$md_array[$i]= $newdata;
$x++;
}
return $md_array;
}
?>
Results like this
--------------------------
Number - 2991
average - 5.25
is_heavy - No
--------------------------
Number - 2992
average - 5.5
is_heavy - No
--------------------------
Number - 2993
average - 5.75
is_heavy - No
--------------------------
Number - 2994
average - 6
is_heavy - No
--------------------------
Number - 2995
average - 6.25
is_heavy - No
--------------------------
Number - 2996
average - 6.5
is_heavy - No
--------------------------
Number - 2997
average - 6.75
is_heavy - No
--------------------------
Number - 2998
average - 7
is_heavy - No
--------------------------
Number - 2999
average - 7.25
is_heavy - Yes
--------------------------
You have return value in every loop so it will only get last index values ! So you need to return outside of the loop ....
function heavyDecC ($x,$y)
{
$result = array();
for($i=$x;$i<=$y;$i++)
{
$numb = $i;
$numleng = strlen((string)$i);
$int = 0;
for($j=0;$j<$numleng;$j++)
{
$int = $int + ($numb % 10);
$numb = (int)$numb / 10;
}
$avg = 0.00;
$avg = round($int/$numleng,2);
if($avg>7)
{
$isHeavy = "Yes";
}
else
{
$isHeavy = "No";
}
$result[] = array($i,$avg,$isHeavy);
}
return $result; // return here outside of loop
}
Move your return outside of the for loop, like this:
function heavyDecC ($x,$y)
{
$result = array();
for($i=$x;$i<=$y;$i++)
{
$numb = $i;
$numleng = strlen((string)$i);
$int = 0;
for($j=0;$j<$numleng;$j++)
{
$int = $int + ($numb % 10);
$numb = (int)$numb / 10;
}
$avg = 0.00;
$avg = round($int/$numleng,2);
if($avg>7)
{
$isHeavy = "Yes";
}
else
{
$isHeavy = "No";
}
$result[] = array($i,$avg,$isHeavy);
}
return $result;
}

Check if Variable is smaller than others, and which one its smaller than. PHP

I have been working on my scoring system and came accros this.
I have 4 vars.
$newscore
$score1
$score2
$score3
I want to see if new score is lower than the 3 others, and if so which ones. The scoring system requires you to have the lowest possible score.
I have the following code:
if($newscore > $score1){
if($newscore > $score2){
if($newscore < $score3){
//has to be score3 to replace.
}
}else{
...
}
}
But what I'm wondering is if I will have to continue on with all these if statements, or is there something a lot shorter and easier? I need to replace the the score that it is smaller than, but not the one its larger than. Score 1 2 and 3 are all the players stats. And if I do have to continue on with all the if statements, how would the code look (its baffling my logic)?
you should probably use an array
$scores = array(8, 15, 10); //previous scores
$new_score = 5;
$new_score_smallest = true;
foreach($scores as $score) {
if($score < $new_score) {
$new_score_smallest = false;
}
}
if($new_score_smallest) {
echo "Best score!";
}
else {
echo "Not the best score :(";
}
If you only want to remember the 3 best scores:
$scores = array(5, 6, 8);
$new_score = 7;
for($i = 0; $i < count($scores); $i++) {
if($new_score < $scores[$i]) {
$scores[$i] = $new_score;
break;
}
}
You could do something like the following:
EDIT: As you're using a database, you would execute the query similar to this:
SELECT scores FROM scores_table replacing the table name and column name with your corresponding data.
$scores = [$score1, $score2] // add as many as you like
$new_score = $scores[0]; // assign a baseline
foreach ($scores as $score) {
if ($score < $new_score) {
$new_score = $score;
}
}
Hope that helps.
You can use array_search and min
$newscore = 2;
$scores = array($score1, $score2, $score3);
if($newscore < min($scores)){
$scores[array_search(min($scores), $scores)] = $newscore;
}
array $score lowest score will be updated if $newscore is inferior
Put your scores in an array or anything iterable (like a query result, whether you use PDO or mysqli).
Let's say your scores are in $scores array (it will be the same if $score is a PDOStatement for example) and ordered (use ORDER ASC in your query):
$scores = [105, 201, 305];
$newscore = '186';
$hiscore = false;
$beaten = [];
foreach ($score as $k => $score) {
if ($newscore > $score) {
$hiscore = true;
$beaten[] = $score;
unset($scores[$k]);
}
}
if ($hiscore) {
echo 'New high score!'.PHP_EOL;
echo 'Better than '.implode(', ', $beaten).PHP_EOL;
echo 'But not better than '.implode(', ', $scores);
} else {
echo 'Try harder!';
}
<?php
$newscore = 78;
$score1 = 23;
$score2 = 201;
$score3 = 107;
$max = max([$score1, $score2, $score3]);
if ($max < $newscore) {
echo "New best score ! ({$newscore})";
} else {
echo "Not the best score !\nCurrent: {$newscore}\nBest: {$max}";
}

Split total into 5 different numbers

Ok what i'm wanting to do is split a number ($row['count']) into 5, this is easy enough if you want equal numbers:
$sum = ($row['count'] / 5);
$fsum = floor($sum);
but I want each number to be different and still add up to total ie $row['count'] how can this be achieved?
Update:
If this helps its to be used to update 5 rows in a database:
$query = "SELECT * FROM foo";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$sum = ($row['count'] / 5);
$fsum = floor($sum);
$id = $row['id'];
$update = "UPDATE foo SET foo1='$fsum', foo2='$fsum', foo3='$fsum', foo4='$fsum', foo5='$fsum' WHERE id='$id'";
mysql_query($update);
}// while
so ideally the $update query would be something like:
$update = "UPDATE foo SET foo1='$fsum1', foo2='$fsum2', foo3='$fsum3', foo4='$fsum4', foo5='$fsum5' WHERE id='$id'";
This is my take:
function randomize($sum, $parts) {
$part_no = count($parts);
$continnue_counter = 0;
while (count(array_unique($parts)) != $part_no) {
$changing = array_rand($parts, 2);
if (($parts[$changing[0]] - 1) == 0 || ($parts[$changing[1]] - 1) == 0) { // don't let them go under 1
++$continnue_counter;
// sometime one element get everything and others even out on 1
// just throw away everything you got so far and start over
if ($continnue_counter > 10) {
$parts = setup($sum, $part_no);
$continnue_counter = 0;
}
continue;
}
$continnue_counter = 0;
$signum = mt_rand(0, 100) % 2 ? 1 : -1;
$delta = $signum * mt_rand(1, min($parts[$changing[0]] - 1, $parts[$changing[1]] - 1)); // -1 to make sure they don't go under 0
$parts[$changing[0]] += $delta;
$parts[$changing[1]] -= $delta;
}
return $parts;
}
function setup($sum, $part_no) {
$parts = array_fill(0, $part_no, (int)($sum / $part_no));
// acount for the reminder of (int) cast
$reminder = $sum - array_sum($parts);
while ($reminder) {
$parts[array_rand($parts)] += 1;
--$reminder;
}
return $parts;
}
$part_no = 5;
$sum = 42;
$parts = randomize($sum, setup($sum, $part_no));
var_export($parts);
print array_sum($parts)
Update:
I've added a version that introduces a little more entropy.
Update2:
The more random one had a tendency to decrement everything to 1 except one part, added an explicit detection to deal with this. Still the algorithm behind it has unknown termination time.

Pseudo-code for shelf-stacking

Suppose I have some serially numbered items that are 1-n units wide, that need to be displayed in rows. Each row is m units wide. I need some pseudo-code that will output the rows, for me, so that the m-width limit is kept. This is not a knapsack problem, as the items must remain in serial number order - empty spaces at the end of rows are fine.
I've been chasing my tail over this, partly because I need it in both PHP and jQuery/javascript, hence the request for pseudo-code....
while (!items.isEmpty()) {
rowRemain = m;
rowContents = [];
while (!items.isEmpty() && rowRemain > items[0].width) {
i = items.shift();
rowRemain -= i.width
rowContents.push(i);
}
rows.push(rowContents);
}
Running time is Θ(number of items)
Modulus is your friend. I would do something like:
$items = array(/* Your list of stuff */);
$count = 0;
$maxUnitsPerRow = 4; // Your "m" above
while ($item = $items[$count]) {
if ($count % $maxUnitsPerRow == 0) {
$row = new row();
}
$row->addItemToRow($item);
$count++;
}
Here is an alternative php code ...
function arrayMaxWidthString($items, $maxWidth) {
$out = array();
if (empty($items)) {
return $out;
}
$row = $maxWidth;
$i = 0;
$item = array_shift($items);
$row -= strlen($item);
$out[0] = $item;
foreach ($items as $item) {
$l = strlen($item);
$tmp = ($l + 1);
if ($row >= $tmp) {
$row -= $tmp;
$out[$i] = (($row !== $maxWidth) ? $out[$i] . ' ' : '') . $item;
} elseif ($row === $maxWidth) {
$out[$i] = $item;
++$i;
} else {
++$i;
$row = $maxWidth - $l;
$out[$i] = $item;
}
}
return $out;
}
For what it's worth, I think I have what I was looking for, for PHP - but not sure if there is a simpler way...
<?php
// working with just a simple array of widths...
$items = array(1,1,1,2,1,1,2,1);
$row_width = 0;
$max_width = 2;
echo "Begin\n"; // begin first row
foreach($items as $item=>$item_width) {
// can we add item_width to row without going over?
$row_width += $item_width;
if($row_width < $max_width) {
echo "$item_width ";
} else if($row_width == $max_width) {
echo "$item_width";
echo "\nEnd\nBegin\n"; // end last row, begin new row
$row_width = 0;
} else if($row_width == 2* $max_width) {
echo "\nEnd\nBegin\n"; // end last row, begin new row
echo "$item_width";
echo "\nEnd\n"; // end new row
$row_width = 0;
if($item < count($items)) echo "Begin\n"; // new row
} else if($row_width > $max_width) {
echo "\nEnd\nBegin\n"; // end last row, begin new row
echo "$item_width";
$row_width = $item_width;
}
}
echo "\nEnd\n"; // end last row
?>

Categories