I have a databse that has food reviews on it, and I want to show if a food is vegetarian or not.
I want to add a echo "No"; but I don't know how.
I've tried using else, but it doesn't show. I've also tried <?php for($x=1;
Here is my code:
<p>Vegetarian: <span class="sub_heading"><?php for($x=0; $x < $find_rs['Vegetarian']; $x++)
{
echo "Yes";
}
; ?></span></p>
I want it to show no if it is 0 and yes if it is 1
<p>
Vegetarian:
<span class="sub_heading"><?= $find_rs['Vegetarian'] ? "Yes" : "No"?></span>
</p>
Simply add an if conditional inside of your loop that checks against the value of $x:
for ($x=0; $x < $find_rs['Vegetarian']; $x++) {
if ($x == 1) {
echo "Yes";
}
else if ($x == 0) {
echo "No";
}
else {
echo "x wasn't either 0 or 1 -- x was $x";
}
}
Note that the final else condition will output the value of $x due to the use of double-quotes.
if the condition only 2, just using if else statement,
<?php
for($x=0; $x < $find_rs['Vegetarian']; $x++){
if($x == 1){echo 'Yes';}else{echo 'No';}
}
?>
Related
<?php
$x = 1;
if ($x == 2)
print "hi" ;
else if($x = 2)
print $x;
else
print "how are u";
?>
Apologies for this basic question as I am a beginner at php.
I was expecting the else statement to be executed and print "how are u", but it executed the elseif statement and printed '2' instead. May I ask why does $x become assigned to 2? Thanks in advance.
<?php
$x = 1;
if ($x == 2)
print "hi" ;
else if($x = 2) /* you assign $x 2 and it's true */
print $x;
else
print "how are u";
?>
In the elseif you are asigning the number 2 to $x which will always return true. Because of that it will never go to the else block.
Dont use = when doing comparisons but == or ===.
<?php
$x = 1;
if ($x == 2)
print "hi" ;
else if($x == 2)
print $x;
else
print "how are u";
?>
this is the right way to do it. Also it doesnt make sense to check both in if and elseif that $x == 2 since it will never execute the elseif block.
Let me try to explain it better, if you do:
if ($x = 2)
thats will assign 2 to $x and this is then the same as:
if ($x)
since the $x now holds the number 2 this is also the same as:
if (2)
this is always true so it will never go to further checks no matter what the number is.
You are SETTING x=2 in your else if --> else if($x = 2) -- This would be more appropriate .. Check for absolute === .. Then check for truthy == .. IE
<?php
$x = 1;
if ($x === 2)
print "hi" ;
else if($x == 2)
print $x;
else
print "how are u";
?>
Conversely .. You can play with truthy vs absolute by comparing an integer to string too .. Like:
<?php
$x = 2;
if ($x === 2) // Will return true
print "Is absolute integer" ;
else if($x == 2) // Will return true
print "Is truthy integer";
else if($x === '2') // Will return false
print "Is absolute string";
else if($x == '2') // Will return true
print "Is truthy string";
else
print "how are u";
?>
Which will never reach the else .. But you can see how operators can make or break a program ..
This is because = is assignment operator not comparison like ==. That is why $x is assigned value 2 and the else if condition is met and executed.
I have a project where I have inputs that decide the range of numbers echoed but I also need to have a single number echo more text. At the moment I have an if statement which works but echoes the number a second time.
for ($x = $var1; $x <= $var2; $x += $varInc) {
echo "<p>$x</p>";
if ($x == $varGhost){
echo "<p class='fas fa-ghost'></p>";
}
}
It's supposed to look similar to this:
You should change your code to:
for ($x = $var1; $x <= $var2; $x += $varInc) {
if ($x == $varGhost){
echo "<p>$x - GHOST!</p>";
} else {
echo "<p>$x</p>";
}
}
I hope it works :)
It isn't working as expected because the second line echoes the number $x in any case, regardless the following if statement:
for ($x = $var1; $x <= $var2; $x += $varInc) {
echo "<p>$x</p>"; // this line echoes the number $x in any case
if ($x == $varGhost) {
echo "<p class='fas fa-ghost'></p>";
}
}
You should check the value of $x before printing it like this:
for ($x = $var1; $x <= $var2; $x += $varInc) {
if ($x == $varGhost) {
echo "<p class='fas fa-ghost'></p>";
}
else {
echo "<p>$x</p>"; // this line echoes the number $x ONLY if it isn't a ghost
}
}
Anyway, a shorter way to write it:
for ($x = $var1; $x <= $var2; $x += $varInc)
echo ($x == $varGhost) ? "<p class='fas fa-ghost'></p>" : "<p>$x</p>"
Hope it helps :)
I am trying to retrieve the last names of the orders placed.
code:
for ($x = 0; $x < 25; ++$x) {
if (($orders[$x]['paymentStatus'] == 'paid') || ($orders[$x]['paymentIsPost'] == TRUE)) {
echo '<pre>';
print_r($orders[$x]['lastname']);
echo '</pre>';
}
}
question: code is not showing me the first lastname in the array actually the name that is in position $orders[0]. I thought that pre-incrementing the variable like this ++$x like this would solve the problem. To bad thats not the case. Any tips?
Update output:
[1]""
[2]""
[3]""
[4]""
[5]""
Not sharing lastnames, which is also not relevant.
try the following to confirm which element of the array you are displaying (and the value of $x
for($x = 0; $x < 25 ; $x++){
if(($orders[$x]['paymentStatus'] == 'paid') || ($orders[$x]['paymentIsPost'] == true)){
echo '<pre>';
--print_r($orders[$x]['lastname']);
echo '['.$x.']'.$orders[$x]['lastname'];
echo '</pre>';
}
}
How to execute php if condition once time only and didn't check it again, i put if condition in for loop and i want to check it one time only:
<?php
for($x=0;$x<3;$x++){
if($x == $x){
echo "";
}
else{
echo "hidden";
}
}
I need to execute first if one time only in for loop
I need to execute first if one time only in for loop. Well that's not how you do it, you need to compare with a variable and upon success just use break to come out of the loop. Something like:
<?php
$y = 0;
for($x=0; $x<3; $x++){
if($x === $y){
echo "X is Equal to Y";
break;
}
else {echo "hidden";}
}
echo "<br>". "You're outside the loop!";
?>
Set a flag, if flag is true don't execute if condition.
<?php
$flag = 0;
for($x=0;$x<3;$x++) {
if($x == $x && $flag == 0){
echo "";
$flag = 1;
}
else{
echo "hidden";
}
}
<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i==1 || ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i=$page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Trying to build a webpage that ouputs some search values neat the bottom of the page, however I keep getting an infinite loop. I've no Idea about what's causing it, and would like someone elses insight into the problem at hand.
Your if condition has a problem
Change
$i =1
with
$i==1
You have a number of places that you reset $i, inadvertently I assume
$i=1
and
$i=$page
replace them with ==
<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i == 1|| ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i == $page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Changes in 1st and second if condition.
1) $i == 1
2) $i == $page
is there a reason why this can not be accomplished using a for loop ? While loops are always risky and easy to cause problems if there is a scenario to cause to loop for ever.
eg.
for ($i = 0; $i<=$totalpages; $i++){
// do something
}
There is some issues in the conditions
first: the
|| $i=1||
allways return true.
Change for
|| $i==1||
Second: The same case of adobe
if ($i=$page) //Allways return try is assignation operation
Change to:
if ($i==$page)