Check if empty before foreach

n00bly asked
php wordpress

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.

Share This
Posted in: