if else does not work - php

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)

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 stop code and put another code if button is submit in the same file

I have a foreach loop and I want to execute always except when a button clicked and then echo something else in the spot that foreach was...I have tried these but none of them work right!
if (!isset($_POST['myBtn'])) {
foreach($postarray as $p){
echo $p->postmarkup();
}
} else {
echo $search_output; //this is the echo that i want to replace the foreach if the button is clicked.
}
OR this
if (isset($_POST['myBtn'])) {
foreach($postarray as $p){
echo $p->postmarkup();
}
exit();
echo $search_output;
}
and I have tried with many other ways but I could not find it... I know that these code are completely wrong :P I am sorry for this, just wanna give you an example to understand what my problem is. If anyone know this ..free to ask :) Thanks in advance!
try this :
foreach($postarray as $p){
if (!isset($_POST['myBtn'])) {
echo $p->postmarkup();
}else{
echo $search_output;
break;
}
}

If MySQL value empty echo, else echo?

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.

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

Need help in display members name when logged in using PHP?

for some r.eason I cant display a logged in users name when they are logged in? the code is below
<?php
if (isset($_SESSION['user_id'])) {
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else { echo 'something';
}
?>
Thanks every one but i solved it.
Ack! Just look at your code. Do you know what this line is doing?
echo '<?php if (isset($_SESSION[\'first_name\'])) { echo ", {$_SESSION[\'first_name\']}!"; } ?>';
That's so wrong I don't even know where to begin. Just try
echo $_SESSION['first_name'];
And see if that gets you closer to what you want ;)
Make sure you're also calling session_start() before trying to access the variables.
Change your code to:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", " . $_SESSION['first_name']} . '!';
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?>
This is not a valid PHP code. Single quote "'" are not pair up. The block ('{' and '}') are also not pairing up.
The most importantly, the code to show the first name is in a string so it will not be shown.
I think the code you are trying to write is:
<?php
if (isset($_SESSION['user_id'])) {
if (isset($_SESSION['first_name'])) {
echo ", {$_SESSION['first_name']}!";
}
if ($_SESSION['user_level'] == 1) {
echo 'something else';
}
} else {
echo 'something';
}
?&gtl
Is it?
Here are the list of possibilities of the mistakes and make sure that you have corrected them
1) have you set the cookie "first_name" using setcookie method...?
2) Then have u called the session_start() function so that the session variables can be called in that page??
3) Try echo $_SESSION['first_name']... i don understand why you have put the flower brackets coz i never have used them even once in my 15 php projects..

Categories