PHP returning value of 1 [closed] - php

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';
}

Related

PHP - Multiple conditions in while statement [closed]

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.

How to use break in if (Command) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using $i string for limit in foreach loop.
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine'){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
}
if($menu_style == 'normal'){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}
I can't want to use LIMIT in SQL query.
And tell me when i use if($i == 5){ break; } then code display just 4
menu name
Tell me how is display menu name as my required.
Something like that?
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine' && $i <= 5){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
} else if($menu_style == 'normal' && $i <= 4){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}

PHP if clause is not working any ideas why? [closed]

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);
}

Next statement in a While Loop with PHP [closed]

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;
}
}

How to stop the counter from increasing when refreshing the page

Here is my code:
$score=0;
$sql10=mysql_query("select * from image WHERE fromUser='".$_SESSION['email']."' order by id desc");
$score1=mysql_fetch_array($sql10);
$answer=$score1['Correct_answer'];
echo $answer;
$sql13=mysql_query("select * from image WHERE toUser='".$_SESSION['email']."' order by id desc");
$score2=mysql_fetch_array($sql13);
$answer2=$score2['User_answer2'];
echo $answer2;
strcasecmp($answer,$answer2);
if(strcasemp)
echo $score +=1;
As u can see I am comparing the answers and if both are equal I want the score to increase by one. But the code is not working any hint???
Looks like you're not using the comparison to increment the score. The if (strcasecmp) line doesn't do anything. You're doing the strcasecmp($answer,$answer2) above, but not storing or using the result of the comparison. Also, strcasecmp() returns 0 for equality. Instead, try:
if(strcasecmp($answer,$answer2) === 0) {
echo $score +=1;
}
To prevent this from incrementing every time you refresh the page, you'll need to store some indication in $_SESSION that you have already processed it.
session_start();
// Check if it hasn't already been answered.
if (!isset($_SESSION['answered']))
{
// Do all your queries to check the answers.
// etc...
// etc...
if(strcasecmp($answer,$answer2) === 0) {
echo $score +=1;
// Store an indicator in $_SESSION so this doesn't get processed again
$_SESSION['answered'] = TRUE;
}
}

Categories