Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if ($subtotal1 != 0 or $subtotal2 != 0 or $subtotal3 != 0 or $subtotal4 != 0 )
{
echo $compno.' '.$subtotal1.' '.$subtotal2.' '.$subtotal3.' '.$subtotal4;
echo "<br>";
$info4 =array($compno, $total);
}
The if block is getting executed even if one of the values of the $subtotal(n) is 0.
Any idea why?
You need to change "or" to "and".
if ($subtotal1 != 0 and $subtotal2 != 0 and $subtotal3 != 0 and $subtotal4 != 0 )
Use && instead of or, cause your if condition will get executed even one of your value is non-zero.
Using or in if statement simply means if any of the condition is true, execute the code.
Using && means execute the if statement if all the conditions are true. Thanks it make it more clear
if ($subtotal1 != 0 && $subtotal2 != 0 && $subtotal3 != 0 && $subtotal4 != 0 )
{
echo $compno.' '.$subtotal1.' '.$subtotal2.' '.$subtotal3.' '.$subtotal4;
echo "<br>";
$info4 =array($compno, $total);
}
Try this
if($subtotal1 != 0){
}
elseif($subtotal2 != 0){
}elseif($subtotal3 != 0)
{
}
elseif($subtotal3 != 0)
{
}
else
{
echo $compno.' '.$subtotal1.' '.$subtotal2.' '.$subtotal3.' '.$subtotal4;
echo "<br>";
$info4 =array($compno, $total);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
There is a block of conditions.Help to shorten and improve this part of the code
$stock = $row['KOLVO_T'];
if ($row['CENA'] < 1000 && $row['KOLVO_T'] == 1) {
$stock = 0;
}
if ($row['CENA'] >= 1000 && $row['KOLVO_T'] == 1) {
$stock = $row['KOLVO_T'];
}
if ($row['KOLVO_T'] >= 2) {
$stock = $row['KOLVO_T'];
}
return $stock;
I don't really see the point, but here's the simplest I could do:
$stock = $row['KOLVO_T'];
if ($row['CENA'] < 1000 && $stock == 1) {
$stock = 0;
}
return $stock;
The last two conditional blocks are useless since they repeat an operation you already did before the first conditional block, and their condition can't be verified if the first is.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
For some reason this code won't run in my compiler. The intention (as part of a larger project) is to ping a particular host 3 times, or until successful, whichever comes first. It's not producing any errors, just terminating. It works just fine if I remove the second condition from the while statement, but then I would need to have something more complicated to terminate the loop on a successful ping. I haven't touched PHP in a while, so I'm probably missing something stupid.
<?php
function pingAddress($ip) {
//Set variable to limit loops and end if live
$pass = 0;
$result = 0;
//Create conditions for while loop
while( ( $pass < 3 ) && ( $result = 0 ) ) {
//Count loops
$pass++;
//Execute ping
$output=shell_exec('ping -n 1 '.$ip);
//Display ping results for testing purposes
echo "<pre>$output</pre>";
//Check for "TTL" presence
if(strpos($output, 'TTL') !== false)
{
//Notate positive result
$result++;
//Display for testing
echo "Alive";
}
//Display negative result for testing
else
{
echo "Dead";
}
}
}
PingAddress("8.8.8.8");
You'll kick yourself:
while( ( $pass < 3 ) && ( $result = 0 ) ) {
Should use a double equals - it's a comparison, not an assignment:
while( ( $pass < 3 ) && ( $result == 0 ) ) {
You don't need the second variable $result. Use break instead.
while($pass < 3) {
//Count loops
$pass++;
//Execute ping
$output=shell_exec('ping -n 1 '.$ip);
//Display ping results for testing purposes
echo "<pre>$output</pre>";
//Check for "TTL" presence
if(strpos($output, 'TTL') !== false)
{
//Display for testing
echo "Alive";
break; //exiting while loop
}
//Display negative result for testing
else
{
echo "Dead";
}
}
You can even write less code with
while($pass++ < 3) {
Your second condition is written incorrectly. Change it to $result === 0
Use equal operator not assign.
while( ( $pass < 3 ) && ( $result == 0 ) )
This should work.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i still cannot get this review system to work properly.
i know the average rating is 3.57 but as soon as i add the if() all i am getting is an output of 1?
// collect 5 lastest reviews for viewing
$sql = "SELECT AVG(rating) FROM reviews WHERE review_id = '$id'";
$query = mysqli_query($conn, $sql);
$AvgReview = mysqli_fetch_row($query);
// Here we have the toatal row count
$AvgReviews = $AvgReview[0];
if($AvgReviews = 1){
echo '1star';
}
//next else is new
else if($AvgReviews >= 1){
echo '2star';
}
//next else is new
else if($AvgReviews >= 2){
echo '3star';
}
//next else is new
else if($AvgReviews >= 3){
echo '4star';
}
//next else is new
else if($AvgReviews >= 4){
echo '5star';
}
You are getting '1star' because you are assigning a value of 1 to $AvgReviews.
Change your code to compare the value.
if ($AvgReviews == 1) {
Additionally, you need to reverse your code or you will end up getting 1 or 2 stars, only.
I would recommend to change it like that:
if ($AvgReviews > 4) {
echo '5star';
}
else if($AvgReviews > 3) {
echo '4star';
}
else if($AvgReviews > 2) {
echo '3star';
}
else if($AvgReviews > 1) {
echo '2star';
}
else {
echo '1star';
}
It's because you're assigning 1 to $AvgReviews. The assignment operator (=) takes the value on the right-hand side, assigns it to the name on the left-hand side, and then returns the value that was assigned. In this case, that's always 1, which PHP interprets as true.
Use the comparison operator instead:
if($AvgReviews == 1){
echo '1star';
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Ok i have a while loop function in my site that pulls in excel documents and parses them to the database i want to check for duplicates and if duplicate skip it:
$content = file($selectfile1);
$posted_content = array();
list($rownum, $row) = each($content);
$posted_content[0] = explode(",", $row);
array_push($posted_content[0], "ID");
$count = 0;
// iterate each row (1 post)
while (list($rownum, $row) = each($content))
{
$count++;
$cols = "orderid, created_at, updated_at, notification_type, radius, available, expiration, ";
$vals = "";
$cols2 = "equipment_id";
$vals2 = "";
....{parsing data)...
}
i want to write in a script that checks to see if the record is a duplicate and if not enter it.
$sql25 = "SELECT * FROM notifications WHERE origin =" . $origin_id . " user_id =12039";
$rs25 = $conn->Execute($sql25);
if($rs25->RecordCount() == 1 || $rs25->RecordCount() >= 1)
{
here is where i need a command. Can you use? next()
--------------------------------------------------
}
else
{
Insert query
}
You are looking for the continue statement.
From the docs:
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
(See http://www.php.net/manual/en/control-structures.continue.php)
example:
<?php
while ( ... ) {
if ($foo = 'bar') {
// skip to the next iteration
continue;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm looking to create a snake draft script. I want the order to go up the array then back down the array.
$teams = array('mike','jim','chris','tim');
I want the order to be
Round 1
Mike, Jim, Chris, Tim
Round 2
Tim, Chris, Jim, Mike
That would be two rounds. I want that to be the order and repeat that but I cant figure it out with PHP.
I have tried getting the last pick from the draft in my database.
$lastpick = (the last team name submitted to my database);
if(empty($lastpick) && $teams(0) == 'mike'){
//do this
}else if($lastpick == $teams(0) && $teams(1) == 'jim'){
// do this
}else if($lastpick == $teams(1) && $teams(2) == 'chris'){
// do this
}else if($lastpick == $teams(2) && $teams(3) == 'tim'){
// do this
}else if($lastpick == $teams(3) && $teams(3) == 'tim'){
// do this
}
What would i do next? this gets the order mike,jim,chris,tim then tim again but I cant get it to back to the third team in my array.
Any help would be great thanks.
I'm not entirely sure why you need && $teams(x) == 'yyy' in each statement, you already know what they are because the order is defined, and if they change, you need to update all these statements.
You will need some form of flag to define which way you're currently moving, up or down.
boolean movingUp = true;
if(empty($lastpick)) { // first team }
else if(movingUp && $lastpick == $teams(0)) { // second team }
else if(movingUp && $lastpick == $teams(1)) { // third team }
else if(movingUp && $lastpick == $teams(2)) { movingUp = false; // fourth team }
else if(!movingUp && $lastpick == $teams(3)) { // fourth team }
else if(!movingUp && $lastpick == $teams(2)) { // third team }
else if(!movingUp && $lastpick == $teams(1)) { // second team }
else if(!movingUp && $lastpick == $teams(1)) { movingUp = true; $lastpick = null; // first team }
You don't need to have a $lastpick == $teams(3) because once you have picked the penultimate one you know the next action, whilst moving up, is the last team.