If MySQL value empty echo, else echo? - php

I am trying to echo "No explanation entered." if the health_info value is empty in the MySQL database with the below code. However, no matter if the row is empty or not, it always echos "No explanation entered." What am I missing or doing wrong? Thanks!!
<?php
if (empty($health_info)) {
echo "No explanation entered.";
} else {
echo $health_info;
}
?>

<?php
if (empty($health_info)) {
echo "No explanation entered.";
} else {
echo $health_info;
}
?>
In your code you are evaluating $health_info that is undefined (if the code is what you posted).
Do a var_dump( $health_info ); and you will know what the value is.

Related

GET request variable error in PHP

I have a simple PHP code, as below.
When I try the URL localhost/df.php?result1=bharat, I get the result Bharat, exactly as I want it. But when I try the URL localhost/df.php?result2=bharat, I get an error, meaning my result2 variable was not read like my result1 variable did.
Could you please correct my code so that it works?
<?php
if(isset($_GET['Result1']))
{
$file = $_GET['Result1'];
}
else
{
echo "Error"; exit;
}
echo "$result1";
?>
elseif(isset($_GET['Result2']))
{
$file = $_GET['Result2'];
}
else
{
echo "Error"; exit;
}
echo "$result2";
?>
You have way too many errors in your code. The following is the solution to your problem:
<?php
if(isset($_GET['result1']))
{
$result1 = $_GET['result1'];
echo $result1;
}
elseif(isset($_GET['result2']))
{
$result2 = $_GET['result2'];
echo $result2;
}
else
{
echo "Error";
exit();
}
?>
For the future, I would recommend you to learn PHP and be familiar with the basic syntax, at least, before posting questions about it here.

how to check if foreach loop empty when using json and php

i'm using JSON to get data and then PHP to display. so...
i'm showing everything available to a person and i want to echo a message when the loop is blank/empty that "there's nothing available" because right now it just shows a blank screen when there is no data... any ideas??
<?
foreach($json['available'] as $r) {
echo '<li>' .$r['item'].'</li>';
}}
?>
Just use an if statement and check if $json['available'] is empty with empty().
if( empty( $json['available'])) {
echo '<li>No items are available</li>';
} else {
foreach($json['available'] as $r) {
echo '<li>' .$r['item'].'</li>';
}
}
Use empty to check if $json contains something or not.
Assuming that $json['available'] is going to be an empty array at worst:
if (!$json['available']) {
echo "nothing to show!";
}
else {
// your current code
}
If it's possible that $json['available'] might not even exist, a more "heavy-handed" alternative is
if (empty($json['available'])) {
echo "nothing to show!";
}

Preventing PHP printing Null Array Results?

I cannot get my PHP script to echo the second else statement if the first result empty.
The way my script currently works is "Print Addresses (from another array) > List Comments", however even if a comment is empty for an address is will either print the comments or nothing, I cannot make the script echo the word "No comment".
if(!empty($row['id']))
{
echo "$row[comment]<br/>";
}
else
{
echo "no comment<br/>";
}
Any help appreciated. Thanks.
Assuming this comes from a database and each row has an id, this will always be true:
if(!empty($row['id']))
Try:
if(!empty($row['comment']))
If id is something else, the same logic applies: check the value that you intend to print:
if (!empty($row['id']) && !empty($row['comment']))
{
echo $row['comment'].'<br/>';
}
else
{
echo "no comment<br/>";
}
EDIT: If this code is looping through all comments attached to a post or something, there will never be any output if there are no comments to loop through. In that case try something like this:
if (count($comments) === 0)
{
echo "no comments<br />";
}
else
{
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
}
else
{
echo "no comment<br />";
}
}
}
OR:
$comment_count = 0;
foreach ($comments as $row)
{
if (!empty($row['comment']))
{
echo $row['comment'].'<br />';
$comment_count++; // We have at least one comment
}
else
{
echo "no comment<br />";
}
}
if ($comment_count === 0) echo 'no comments<br />';

isset($_GET['variable']) not working?

This function is not working for me. I think it is isset($_GET['success']) that's not working but I'm really not sure. the problem is it doesn't print anything ever. And without if(isset($_GET['success'])) it only prints "username taken" Please help?
<?php
if(isset($_GET['success'])) {
$success=$_GET['success'];
if($success=='yes') {
echo "<center><font color='red'>Comment Posted!</font></center>";
}
else {
echo "<center><font color='red'>Username taken!</font></center>";
}
}
?>
What kind of output are you getting from this? Are you passing the GET method correctly? the URL should have page_name.php?success=yes in it. If you're not getting anything and you want success to be only set if it is true perhaps this would be better.
<?php
if(isset($_GET['success']) && $_GET['success']=='yes')
{
echo "<center><font color='red'>Comment Posted!</font></center>";
}
else
{
echo "<center><font color='red'>Username taken!</font></center>";
}
?>

if else does not work

i have problem with if else statement in zend framework
<?php if(count($this->result) > 0) {
echo "not found";
}
else{
echo "advertise here";
}
?>
i would like to hide ads if there is no result, somehow, it does not work, please help
I'm not sure if I've understood the question correctly. But try
<?php
if(count($this->result) > 0) {
echo "advertise here";
}else{
// do nothing..effectively hiding.
}
?>
You can also get rid of the else part completely.
Your if else statement seems to be correct.
Did you try to check the content of your variable with a
var_dump($this->result)

Categories