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.
Related
i am exploring some old php files for the nostalgia, and i noticed this is what ive written before to fetch some records:
<?php
for ( $counter = 0; $row = mysqli_fetch_row( $result );++$counter )
{
print( "<tr>" );
foreach ( $row as $key => $value )
{
if($key == '6')
{
if($value == '1'){print( "<td>12/9/15 6:00PM - 7:00PM</td>" );}
if($value == '2'){print( "<td>12/9/15 7:00PM - 8:00PM</td>" );}
if($value == '3'){print( "<td>12/9/15 8:00PM - 9:00PM</td>" );}
if($value == '4'){print( "<td>12/10/15 6:00PM - 7:00PM</td>" );}
if($value == '5'){print( "<td>12/10/15 7:00PM - 8:00PM</td>" );}
if($value == '6'){print( "<td>12/10/15 8:00PM - 9:00PM</td>" );}
}
else
{
print( "<td>$value</td>" );
}
}
print( "</tr>" );
}
mysqli_close( $database );
?>
i changed it to while ($row = mysqli_fetch_row( $result )) and it works as well, as expected. i am just trying to understand the logic behidn how the for loop method works when $counter isnt being used...
i am scrambling to understand why i used this kind of logic, and how is it even working when $counter isnt even used for indexing or anything.
Easiest way to understand how this is working is by dissecting the for loop.
for (<initialization>; <condition>; <update expresion>)
In your case you have a counter that is being set initially, and incremented.
for ( $counter = 0; $row = mysqli_fetch_row( $result );++$counter )
You aren't using the counter, but it actually doesn't matter since the only thing that determines if the loop continues is if the condition is true. Since eventually mysqli_fetch_row stops returning results, the loop will terminate. The $counter variable is unused and ignored for the purpose of the loop.
You could also rewrite your for loop to look like:
for (;$row = mysqli_fetch_row( $result );) and this would also work the same. But then, what is a while loop? It's just a for loop without an initialization and update expression. Thus you could rewrite this again as:
while ($row = mysqli_fetch_row( $result ))
I hope this clears things up.
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';
}
This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 8 years ago.
$i=0;
$sql = $dbh->prepare('SELECT kappaleen_nimi, levy_id, kesto FROM kappaleet');
$ok = $sql->execute();
if(!$ok) { print_r( $sql->errorInfo() ) ; }
while($row = $sql->fetch(PDO::FETCH_ASSOC) ) {
foreach($row as $value) {
if($value!=null || $value!=0){
$haku2[$i] = $value;
$i=$i+1;
}
}
}
I'm working with this sql database, where i'm fetching kappaleen_nimi (name of the track), levy_id (record_id) and kesto (length) from the sql table kappaleet. I'm trying to remove fields containing null or 0. However, the final zero gets through anyway through this filter: "if($value!=null || $value!=0)".
When printing my table the following way:
foreach($haku2 as $value){
echo $value.', ';
}
I get this kind of result (last few fields): "Sateen tango, 7, 3.11, 0, "
The final zero is still there, and i can't get my head around it, why the he** it passes the if condition... I know that the final zero is levy_id (record_id).
I think you are looking for an AND conditional instead of an OR. To fix this, replace the following line
if($value!=null || $value!=0){
with this one
if($value!=null && $value!=0){
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);
}
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;
}
}