Strange thing on PHP simple IF - php

In my code there is some IFs that all are the same and working, Except one!
Here is the code
<?php
if($tmp['data']=='0')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='1')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='a')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='b')
{
?>
some code...
<?php
}
?>
<?php
if($tmp['data']=='c')
{
?>
some code...
<?php
}
?>
$tmp['data'] is a value that is fetched from SQL database (varchar type).
IFs are working for all values, but when I set the value in phpmyadmin to 'c', related IF doesn't execute.
Any idea?

You should really use else if if more than one condition is never going to be true.
to debug try to comment everything else and just keeping the if of c, make sure it is not case sensitive (eg. server may return C, but you may check for c)
Try this peice of code to see if it works (though its bit complicated)
if( strcasecmp((string)$tmp['data'],"c")==0)
{
.. code here
}

Related

display different results based on array value

I am trying to display a custom HTML page if the user that requests the page has the [is_vip] value equal to "5".
this is the line I'm trying to run:
<?php if ($membership["is_vip"] == 5) { ?>
do stuff
<?php } else {
do other stuff
However it's not displaying the custom results properly, the contents of $membership look like this when printed.
Array
(
[0] => napify\Model\Membership Object
(
[is_vip] => 5
)
)
This should be simple but i've been stuck in this for hours.
That's an integer indexed array with one element containing an object. Plus the code you posted won't parse and is a parse error. Once you fix the syntax:
<?php if ($membership[0]->is_vip == 5) { ?>
do stuff
<?php } else { ?>
do other stuff
<?php } ?>
Make sure to use error reporting when developing:
error_reporting(E_ALL);
ini_set('display_errors', '1');
I believe AbraCadaver is correct with their post
<?php if ($membership[0]->is_vip == 5) { ?>
but there is also an issue with your closing tag.
<?php } else {
should be
<?php } else { ?>
and your else statement needs to be closed with a
<?php } ?>

Pass $_SESSION to next statement

I developing a Intranet and I'm a bit stuck with allowing access for individual users. All works fine when I limit the access to an element, if that element in at the bottom/last element. I need this to usable where ever I want. If you are in the Directors group, get the element. If you are in the All group only, you get nothing. Any help would be great.
The HTML:
.....
<?php include('admin/Directors.php');
echo 'foooooo':
?>
....
<?php include('admin/All.php');
echo 'baaaar':
?>
...
The PHP (Directors.php):
<?php
session_start();
$allowed_users = array('mark','joe','allan');
if(!in_array($_SESSION['user'],$allowed_users)) die('');
?>
From wat I understand is happening here is that its reading the Directors.php file and applying it to entire HTML file.
Try this In your Directors.php:
session_start();
$allowed_users = array('mark','joe','allan');
return in_array($_SESSION['user'],$allowed_users));
And this in your html:
$allowed = include('admin/Directors.php');
if($allowed)
{
echo 'foooooo';
}
Instead of killing script with die() simply return the evaluation value check it in your html. But if there is other stuff in Director.php you can do this.
Try this In your Directors.php:
session_start();
$allowed_users = array('mark','joe','allan');
$allowed =in_array($_SESSION['user'],$allowed_users));
And this in your html:
include('admin/Directors.php');
if($allowed)
{
echo 'foooooo';
}

Don't know how to display error when using foreach

I'm working (for the firs time) with MVC model and I'm having some problems when displaying the errors warnings...
For example I got this
Controller:
include_once(UserModel.php);
$userdata = new User_Model();
$biography = $userdata->Get_UserBio();
Model:
include_once(conexion.php);
//I make my prepared query and...
if ($user_infoprofile->execute()):
$data= $user_infoprofile->get_result();
if ($data->num_rows):
while ($result=$data->fetch_array()):
$this->userbio[]=$result;
endwhile;
else:
//I want to display something like
$error= "you have to fill your bio",
echo $error;
endif;
else:
$error="invalid request";
echo $error;
endif;
return $this->userbio;
$user_infoprofile->close();
View:
include_once(UserController.php);
//if there are rows in the array I display
foreach...
//But heres my problem... if there are not rows in the array... I want to display something else
How can I do it? I'm kind of beginner on this, or is there any other more clean way to do it?
Use quotes in your require functions. Like this : include_once('UserModel.php');. If you want to display all errors, you can do it with this lines :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(empty($var_array)){
echo "There is not results...";
}else{
foreach....
}
Do you mean how to check if the array is empty?
ok but, the problem is how to doit in a specific part of my view.php file... because If I use 'echo' this message will appear anywhere and not where I would want to...

Multiple If Statements using variable

I am trying to make some logic controls on input variables $ContentPicture1Title and $ContentPicture1URL. In particular I am trying to get two rules applied in a single if statement.
In the first piece of code was everything ok. Then I messed out the code as you can see in the second snippet.
First statement (OK):
<?php if (is_null($ContentPicture1Title)){ ?>
<div class="letter-badge"><img src="image1.jpg"></div>
<?php } else { ?>
<div class="letter-badge"><img src="image2.jpg"></div>
<?php }?>
Second (messed) statement:
<?php if (is_null($ContentPicture1Title) && ($ContentPicture1URL)){ ?>
<div class="letter-badge"><img src="image1.jpg"></div>
<?php } else { ?>
<div class="letter-badge"><img src="image2.jpg"></div>
<?php }?>
Thank you in advance for your help.
Unless $ContentPicture1URL is a boolean variable, you need to invoke a function that can be evaluated as a boolean and/or compare it to another variable/value.
e.g.
if(is_null($variable1) && is_null($variable2)) {
//Do something
}
or
if(is_null($variable1) && $variable2 == 5) {
//Do something
}
As you can read in official documentation you need to use the second construct:
<?php if (is_null($ContentPicture1Title) && $ContentPicture1URL): ?>
<div class="letter-badge"><img src="image1.jpg"></div>
<?php else: ?>
<div class="letter-badge"><img src="image2.jpg"></div>
<?php endif; ?>
This will run if $ContentPicture1URL is a boolean, otherwise you need to use compare operators ( ==, !=, &&, ||, etc.) or boolean functions (is_null()) in order to verify the condition properly.

php if foreach statement works, but else message not displaying

I'm getting an array of data from my model, and am using a php foreach statement to display it in a view. I'm adding another layer of logic to show only certain bookmark_ids.
The data is displaying fine; but for some reason the "else" message (tied to the first if clause) isn't showing if no data is returned. I need to figure out how to get that message to display.
<?php
if ($bookmark):
foreach($bookmark as $b): ?>
<?php if ($b->bookmark_id != 0) { ?>
<li><?php echo $bk->user_id; ?> <?php echo $bk->bookmark_name; ?></li>
<?php } ?>
<?php
endforeach;
else:
print "Your bookmark list is empty.";
endif;
?>
You a testing if $bookmark exists! I assume that it always exists, either empty or with array of values!
Try this:
<?php
if (is_array($bookmark) && count($bookmark)>=1):
foreach($bookmark as $b): ?>
<?php if ($b->bookmark_id != 0) { ?>
<li><?php echo $bk->bookmark_name; ?></li>
<?php } ?>
<?php
endforeach;
else:
print "Your bookmark list is empty.";
endif;
?>
Read: PHP is_array() | count()
EDITED
Related to the recently posted comment "Yes, the array is returning results; I am using the second if statement to limit what is shown. It sounds like my else statement should be tied to the second if clause, instead of the first. The issue for me isn't whether there are results; it's whether after the results are filtered anything remains.":
<?php
// reset variable
$count = 0;
// if it is an array and is not empty
if (is_array($bookmark) && count($bookmark)>=1):
foreach($bookmark as $b):
if ($b->bookmark_id != 0) {
echo '<li>' . $bk->bookmark_name . '</li>';
} else {
$count++; // no data, increase
}
// check if the counter was increased
if ($count>=1) {
print "Your bookmark list is empty.";
}
endforeach;
else:
print "bookmark not found.";
endif;
?>
For one reason or another, $bookmark is evaluating to true. Since empty strings and arrays already evaluate to false in PHP, we might reasonably suppose that $bookmark is in fact an object. Since we can iterate over it with foreach, it is probably an instance of ArrayObject, or a class that extends it. We could debug the exact type of $bookmark by writing:
var_dump($bookmark);
Since an empty object instance evaluates to true in a PHP conditional, we need to be more specific about what we're checking for.
if(count($bookmark) > 0):
This should trigger the else condition properly. As a side note, you should really indent your code properly. :)
if (is_array($bookmark) && !empty($bookmark)) {
foreach ($bookmark as $item) {
...
}
} else {
echo "Your bookmark list is empty.";
}

Categories