display different results based on array value - php

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

Related

Check if empty before foreach

I'm modifying some code but I'm not an expert on php level and need some help :)
<div class="emd-main emd-main-<?php echo $emd_state; ?>">
<?php if ( $userpro->memberlist_in_search_mode($args) ) { ?>
<?php $arr = $userpro_emd->users( $args );
if (isset($arr['users']) && !empty($arr['users']) ) {
?>
<?php if (isset($arr['paginate']) && $args['emd_paginate'] && $args['emd_paginate_top'] == 1) { ?>
<div class="userpro-paginate top"><?php echo $arr['paginate']; ?></div>
<?php } ?>
<div class="emd-list" data-layoutmode="<?php echo $args['emd_layout']; ?>">
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
<?php $tk_image_1 = get_field('foto_1', 'user_'. $user_id); ?>
<?php if (!empty($tk_image_1)) { ?>
<div class="emd-user">
I have the following issue; I have a page that shows a grid with members. The number of members per page is 20 (that is set within the plugin settings). The foreach is starting with this:
<?php foreach($arr['users'] as $user) { $user_id = $user->ID; ?>
After that I check if the value $tk_image_1 is not empty, if not empty then go on. So far so good.
The only thing now is; when I have 20 members on a page and for 8 of them the $tk_image_1 is empty then it shows 12 members on that page... I think it's something in the array that counts them before checking the $tk_image_1 value.
What is need is to show 20 members per page and only the one if $tk_image_1 is not empty.
Can someone help me with this?
Many thanks!
Regards,
Robert
The source of your problem is that you're working with a set of results that contains users you don't want to show. To properly correct this, you need to alter the query that's taking place in this code fragment: $userpro_emd->users( $args )
That ->users() method may accept additional $args that would allow you to say, "only users who have foto_1". That way you're not trying to perform magic in the loop, and then be left with the problem of not having enough others to make up for those who were missing foto_1.

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...

Strange thing on PHP simple IF

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
}

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