dongling4288 2017-11-09 13:46
浏览 119

在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

  • 写回答

1条回答 默认 最新

  • douhan1992 2017-11-09 13:51
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值