Some rows aren't counted in a foreach loop - php

I get price from database with 9xx items.
I add this on show items page. Using Foreach $rows=$row. vprice is my sellingprice and dprice is my dealerprice
$commisionrate = 30;
$commisionfee = 100;
$fee = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$z = $x + $y;
$rate = $z;//(100*30%)+100
if (($rate > $row['vprice']) && ($fee < $row['vprice'])){
echo $fee;
}elseif (($fee > $row['vprice']) && ($rate < $row['vprice'])){
echo $rate;
}elseif ($row['dprice']=$row['vprice']){
echo $row['dprice'];
}
when I recheck all, I found that few items of $row['dprice'] is not counted and still show by old price. Example that is false: I found vprice is 188 with 80 dprice after calculate should be 104 but not changing with still stay on 80.

$commisionrate = 30;
$commisionfee = 100;
$fee = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$rate = $x + $y;
// You don't need to put nested brackets, it very simple condition
if ($rate > $row['vprice'] && $fee < $row['vprice']){
echo "fee: " . $fee; //add some hint words, so you know which condition fires
// You don't need to put nested brackets, it very simple condition
} elseif ($fee > $row['vprice'] && $rate < $row['vprice']) {
echo "rate: " . echo $rate;
// USE double `==`, because when single `=` used, the condition always returns true and you're confused by your result
} elseif ($row['dprice'] == $row['vprice']) {
echo "row[\'dprice\']: " . $row['dprice'];
// add last else
} else {
// this is helpful for debugging of your possible output, regardless you are awaiting some output here
}

Related

Comparing results in Gravity Forms

I've created a questionnaire with three categories. Each possible answer in the questionnaire corresponds with one of the categories. I have three admin numeric fields where I add the number of answers selected from each category (fields 121, 122, 123). This part of the form is working. I'd like to compare the totals from these fields to see which is greatest and then return that result to a hidden field (field 126). So far my code is triggering a critical error when I submit the form.
add_filter("gform_pre_submission_9", "find_highest_percent");
function find_highest_percent ($vata, $pitta, $kapha, $form) {
$total = 0;
$vata = $_POST["input_121"] ;
$pitta = $_POST["input_122"] ;
$kapha = $_POST["input_123"] ;
$total = $vata + $pitta + $kapha;
$vata_percent = ($vata / $total) * 100;
$pitta_percent = ($pitta / $total) * 100;
$kapha_percent = ($kapha / $total) * 100;
if (abs($vata - $kapha) <= 10) {
$result = "Vata-Kapha";
} elseif (abs($vata - $pitta) <= 10) {
$result = "Vata-Pitta";
} elseif (abs($pitta - $kapha) <= 10) {
$result = "Pitta-Kapha";
} elseif (abs($vata - $pitta) <= 10 && abs($vata - $kapha) <= 10 && abs($pitta - $kapha) <= 10) {
$result = "Tri-Doshic";
} else {
if ($vata > $pitta && $vata > $kapha) {
$result = "Vata";
} elseif ($pitta > $vata && $pitta > $kapha) {
$result = "Pitta";
} else {
$result = "Kapha";
}
}
$_POST["input_126"] = $result;
}
I've tested by removing all calculations and simply returning the number 100 but this also triggers the error. Grateful for any suggestions.
Does it help to set the variables to integers?
$vata = (int)$_POST["input_121"] ;
$pitta = (int)$_POST["input_122"] ;
$kapha = (int)$_POST["input_123"] ;

I am trying to write a prime number recogniser but for some reason it won't work

It won't recognise the else statement near the bottom and just won't echo it.
<?php
$numval = $_GET["num"];
// see if string is all numbers
if (is_numeric($numval) == 1) {
// if string is all numbers change its type to integer
settype($numval, "int");
// divide numval by every number and see if it is whole
// create a while loop with $i = 2 and $stop = false
// loop dividing $numval by $i,
// if it is decimal then $i++ and repeat
// if a number is found that fits into it then set $stop to true and echo it is not prime
// if nothing is found that fits into $numval then set $stop to true and print
$i = 2;
$stop = false;
while ( $i < $numval && $stop == false) {
if ( floor( $numval / $i ) != $numval / $i ) {
$i++;
} elseif ( floor( $numval / $i ) == $numval / $i ) {
echo "The number is not prime";
$stop = true;
} else {
echo "Number is prime";
$stop = true;
}
}
}
?>
<br>
<p>Enter whole positive number that isn't 0 or 1</p>
<form>
<input name="num" type="text">
<input type="submit">
</form>
Your else condition will never be reached because your if/elseif are true/false. There is no third option. Instead, move the echo outside of the loop, and just set a flag inside of the loop for when it's not prime.
if (is_numeric($numval) == 1) {
// if string is all numbers change its type to integer
settype($numval, "int");
// divide numval by every number and see if it is whole
// create a while loop with $i = 2 and $stop = false
// loop dividing $numval by $i,
// if it is decimal then $i++ and repeat
// if a number is found that fits into it then set $stop to true and echo it is not prime
// if nothing is found that fits into $numval then set $stop to true and print
$i = 2;
$not_prime = false;
while ( $i < $numval && $not_prime == false) {
if ( floor( $numval / $i ) != $numval / $i ) {
$i++;
} elseif ( floor( $numval / $i ) == $numval / $i ) {
$not_prime = true;
}
}
echo $not_prime ? "Number is not prime" : "Number is prime";
}
Your first two conditions are already collectively exhaustive:
if ( floor( $numval / $i ) != $numval / $i ) {
//...
} elseif ( floor( $numval / $i ) == $numval / $i ) {
//...
}
That is, they already cover all possible scenarios. It's logically the same as:
if (true) {
//...
} elseif (false) {
//...
}
There doesn't exist any scenario in which at least one of these two conditions is not satisfied. So any additional elseif or else blocks are superfluous. They're allowed because they are syntactically valid, but will never be reached because the logic for them will never be satisfied.

How to validate decimal stepping from a starting number in PHP

I need to validate that an inputted number is a valid number based on my stepping rules and round up to the nearest valid number if not. These numbers will change but one example would be:
$min = 0.25;
$step = 0.1
$qty = 0.75 // user input
so these would be valid inputs:
0.75
0.85
0.95
But these should round:
0.76 (to 0.85)
0.80 (to 0.85)
I thought I could use modulus somehow but not getting the calculation correct.
if (($qty % min) / $step == 0)) {
echo "good";
}
I've tried some variations of math that are likely very wrong
$step = 0.1;
$min = 0.25;
$qty = .85;
$h = ($qty / $min) / $step;
echo $h;
$j = mround($qty, $min-$step);
echo $j;
function mround($num, $parts) {
if ($parts <= 0) { $parts = 1; }
$res = $num * (1/$parts);
$res = round($res);
return $res /(1/$parts);
}
I think you can use fmod to do this.
$new = $original + ($step - fmod($original - $minimum, $step));
Example on 3v4l.org

Use PHP while loop to create advance filter

I have a problem and that is I want to create a link on a website like people can click the link to show certain products only depending on percentage. like for example, i have a column in my database with discount percentage and it will show min discount and max discount. assuming we have min and max discount. $min=12 and $max=94; and I want to put them in links to show only products with certain discounts only like filtering. below is the example of the link.
<a href="#">12% to 20%</a
21% to 30%
31% to 40% and so on until it reaches
81% to 90% and the last will be
91% to 94%
smallest and largest numbers will be coming from a column from database and they can change frequently. i came up with solution and its working fine but my code is too long and its like I took to many steps which could be done in few lines of code. I have pasted my working code below but I am sure this can be reduced to few lines of code.
$catsql25 = "SELECT MAX(down_percentage) as largest FROM hot_deals";
$catquery25 = mysqli_query($conn, $catsql25);
while ($row25 = mysqli_fetch_array($catquery25, MYSQLI_ASSOC)){
$largest_number = $row25['largest'];
}
$catsql26 = "SELECT MIN(down_percentage) as smallest FROM hot_deals";
$catquery26 = mysqli_query($conn, $catsql26);
while ($row26 = mysqli_fetch_array($catquery26, MYSQLI_ASSOC)){
$smallest_number = $row26['smallest'];
}
$array_tens = array(10,20,30,40,50,60,70,80,90,100);
foreach ($array_tens as $value){
if(($value - $smallest_number <= 10) && ($value - $smallest_number > 0)){
echo '<a href="/exp.php?fst='.$smallest_number.'&lst='.$value.'"><div class="lfmen2">';
echo $smallest_number." to ".$value."</div></a>";
$next_num = $value + 1;
$next_ten = 9;
$stop_num = floor($largest_number / 10);
$stop_num2 = $stop_num * 10;
//echo $stop_num2.'<br>';
$num_rounds = $stop_num2 - $value;
$num_rounds2 = $num_rounds / 10;
//echo $num_rounds2;
for ($i = 1; $i <= $num_rounds2; $i++){
$end_num = $next_num + $next_ten;
echo '<a href="/exp.php?fst='.$next_num.'&lst='.$end_num.'"><div class="lfmen2">';
echo $next_num;
echo " to ";
echo $end_num;
echo "</div></a>";
$next_num += 10;
$end_num += 10;
}
}
}
foreach ($array_tens as $value2){
if(($largest_number - $value2 < 10) && ($largest_number - $value2 > 0)){
$lsst = $value2 + 1;
if($lsst != $largest_number){
echo '<div class="lfmen2">'.$lsst." to ".$largest_number."</div>";
}
elseif($lsst == $largest_number){
echo '<div class="lfmen2">'.$largest_number.'</div>';
}
}
}
I know its all mess but..
Thanks.
First thing you could do is only one SQL Query :
$catsql = "SELECT MAX(down_percentage) as largest, MIN(down_percentage) as smallest FROM hot_deals";
And then you'll need only one loop :
$catquery = mysqli_query($conn, $catsql);
while ($row = mysqli_fetch_array($catquery, MYSQLI_ASSOC)){
$largest_number = $row['largest'];
$smallest_number = $row['smalest'];
}
After that, you could make only one foreach loop. The two "if" conditions could be in the same loop :
foreach ($array_tens as $value) {
if (($value - $smallest_number <= 10) && ($value - $smallest_number > 0)) {
echo '<a href="/exp.php?fst='.$smallest_number.'&lst='.$value.'"><div class="lfmen2">';
echo $smallest_number." to ".$value."</div></a>";
$next_num = $value + 1;
$next_ten = 9;
$stop_num = floor($largest_number / 10);
$stop_num2 = $stop_num * 10;
//echo $stop_num2.'<br>';
$num_rounds = $stop_num2 - $value;
$num_rounds2 = $num_rounds / 10;
//echo $num_rounds2;
for ($i = 1; $i <= $num_rounds2; $i++) {
$end_num = $next_num + $next_ten;
echo '<a href="/exp.php?fst='.$next_num.'&lst='.$end_num.'"><div class="lfmen2">';
echo $next_num;
echo " to ";
echo $end_num;
echo "</div></a>";
$next_num += 10;
$end_num += 10;
}
}
if (($largest_number - $value < 10) && ($largest_number - $value > 0)) {
$lsst = $value + 1;
if ($lsst != $largest_number) {
echo '<div class="lfmen2">'.$lsst." to ".$largest_number."</div>";
} elseif ($lsst == $largest_number) {
echo '<div class="lfmen2">'.$largest_number.'</div>';
}
}
}
To make it more readable, you could also comment your code to know what do what.
This and a good indentation and you're right.
Hope it helps.

Check if a variable is in POWER base + PHP

I want to check if an input variable is in POWER base.
For example :
Input : 25 ;//yes in power pow(5,2)
Input : 26 ;//no not in power
I made a code but its time taking
$var = 25;
$half = round($var / 2);
for($x = 0; $x <= $half; $x++){
for($y = 0; $y <= $half; $y++){
if(pow($x, $y) === $var){
$output = "1";
}
}
}
if(!$output){
echo "0";
}else{
echo "1";
}
You can use sqrt which will return square root of given number, then just check if the returned value is in decimal or not.
$var = 25;
$sqrt = sqrt($var);
if($sqrt !== floor( $sqrt )) {
echo 'no power';
} else {
echo 'power';
}

Categories