与foreach制作桌子并同时工作

时间:2016-08-29 04:17:28

标签: php mysql laravel laravel-5 laravel-5.1

这里我想创建一个表格,根据$ kosts-> stock_room中的数据自动生成行数。然后我还想根据房间号码在表residents上用数据直接排列数据,所以如果我在表格中有数据,那么居民就可以了。如果房间号为10,则会在表格中填入10行。

这里是我的代码:

<table class="table table-hover table-striped">
        <tr>
            <th width="10%">Action</th>
            <th>Room</th>
            <th>Kost</th>
            <th>Name</th>
            <th>Due Date</th>
        </tr>        
        <tr>
            @foreach($posts as $row)
            @for($i=1;$i<=$kosts->stock_room;$i++) 
            <tr>
            <td>                
            <a title='Edit Data' class='btn btn-xs btn-warning' href='{{ url("resident/edit/$i") }}'><i class='fa fa-pencil'></i></a>
            <a title='Delete Data' class='btn btn-xs btn-danger' href='{{ url("resident/delete/$i") }}' onclick="return confirm('sure to delete ?')"><i class='fa fa-trash'></i></a>                      
            </td>
            <td>{{$i}}</td>
            <td></td>
            <td>{{$row->name}}</td>
            <td></td>
            </tr>   
            @endfor
            @endforeach
        </tr>
    </table>

有人可以帮助我,让我的代码可以像我想的那样工作吗?

我的错误结果: 在那里我的数据总是循环

image

它应该是这样的:(在那里我只做10行)

should be

我桌上的数据:

table

例如,在那里,我有房间号为9的数据,所以我的数字为9的表填充了来自数据库的数据,房间号为9

我很抱歉我的英文不好

2 个答案:

答案 0 :(得分:0)

试试这个:

@foreach($posts as $row)
@for($i=1;$i<=$kosts->stock_room;$i++)

<tr>
..... // Put <tr> inside loop so that it can generate a new table row for each iteration
</tr>

@endfor
@endforeach

答案 1 :(得分:0)

我认为你已将你的tr添加到错误的位置,因为没有显示任何所需的结构作为我做出更正的假设请尝试使用此代码:

    <table class="table table-hover table-striped">
            <tr>
                <th width="10%">Action</th>
                <th>Room</th>
                <th>Kost</th>
                <th>Name</th>
                <th>Due Date</th>
            </tr>        
@foreach($posts as $row)
            <tr>

                @for($i=1;$i<=$kosts->stock_room;$i++) 
                <td>                
                <a title='Edit Data' class='btn btn-xs btn-warning' href='{{ url("resident/edit/$i") }}'><i class='fa fa-pencil'></i></a>
                <a title='Delete Data' class='btn btn-xs btn-danger' href='{{ url("resident/delete/$i") }}' onclick="return confirm('sure to delete ?')"><i class='fa fa-trash'></i></a>                      
                </td>
                <td>{{$i}}</td>
                <td></td>
                <td>{{$row->name}}</td>
                <td></td>
                @endfor
            </tr>
                @endforeach
        </table>

如果没有帮助那么请将数组的转储和想要显示的所需结构放在