简体   繁体   English

2 表内的 Foreach 循环

[英]2 Foreach Loop Inside A Table

I had Users table and group table, when i load a group table, it loads the user which have same group_id as the group table id.我有用户表和组表,当我加载组表时,它加载与组表 ID 具有相同 group_id 的用户。 it works, but my problem is the foreach was quite a mess.它有效,但我的问题是 foreach 非常混乱。 the output looks like this..输出看起来像这样..在此处输入图片说明

however, i want to make the output look like this.但是,我想让输出看起来像这样。在此处输入图片说明

here is my code..这是我的代码..

 <tbody>
     <?php $i = 0; ?>
     @foreach ($auser as $res)
     <?php $i += 1; ?>
     <tr @if($i%2==0) class="bgcolor" @endif>
         <td>{{ $res->id }}.</td>
         <td id="showdet">{{ $res->nama }}</td>
         <td>{{ $res->description }}</td>
         <?php $user = DB::table('users')->where('devgroup',$res->id)->get();?>
         @foreach($user as $mem)
             <td>{{ $mem->name }}</td>
         @endforeach
         <td>
             <a class="default-btn" href="/pms/admin/group/edit/{{ $res->id }}">Edit</a>&nbsp;&nbsp;
             <a type="submit" class="default-btn del" data-id="devgroup" href="#"{{ $res->id }}">Delete</a>
         </td>
     </tr>
     @endforeach
 </tbody>

what did i do wrong?我做错了什么? thank you for your help.感谢您的帮助。

You create new TD for each member.您为每个成员创建新的TD The nested foreach has to be:嵌套的 foreach 必须是:

<td>
    @foreach($user as $mem)
        {{ $mem->name }}<br>
    @endforeach
</td>

The result will be:结果将是:

<td>
    Name 1<br>
    Name 2<br>
    Name 3<br>
</td>

I don't know the template engine you used, add inside a loop condition and don't put <br> after the last one member.我不知道您使用的模板引擎,在循环条件中添加并且不要将<br>放在最后一个成员之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM