dongshi6844 2016-03-29 10:15
浏览 16
已采纳

在if子句为真的时候迭代一个结果

I have a condition that checks to see it the users id from the users table is the same as the user id that is attached to the post, and after that I want to attach a specific link for each post that meets that conditions with the actual posts data:

<?php if($post->user_id == $first):?>
                <?php foreach($code1 as $k=>$codes1): ?>
                <div class="imgSet">
                  <a href= "modify.php?1u=<?php echo $codes1->code;?>"> <!--hyperlink to edit text -->
                 <img src="img/settings.png"/> <!-- edit logo -->
                  <a/>
                </div>
                 <?php endforeach;?>
               <?php endif; ?>

Example: if $post->user_id== $first, my code will show all the value that $code gets from the database, I want to show one result at a time for each posts where the $post->user_id== $first.

To be more specific, if I have more posts with that meet the same condition in the if clause, I want to attach to that kink the code that is specific to that posts, but my program return for each condition meet all the result from the database.

  • 写回答

1条回答 默认 最新

  • dpb35161 2016-03-29 10:25
    关注

    You can use array_shift to get the first code from the codes array. All you have to assure is that you get the codes in the same order as the posts

    <?php
    foreach ($posts as $post) {
        if($post->user_id == $first) {
            $codes1 = array_shift($code1);
    ?>
    <div class="imgSet">
        <a href="modify.php?1u=<?php echo $codes1->code;?>"> <!--hyperlink to edit text -->
            <img src="img/settings.png"/> <!-- edit logo -->
        <a/>
    </div>
    <?php
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错