Php 如何在Laravel中的一个html表中运行两个foreach循环?
我想在一个表中动态显示两个Php 如何在Laravel中的一个html表中运行两个foreach循环?,php,laravel,loops,foreach,foreach-loop-container,Php,Laravel,Loops,Foreach,Foreach Loop Container,我想在一个表中动态显示两个foreach循环数据,但表的设计在动态循环数据之后中断。 我的blade.php代码如下所示: <table> <tr> <th>Name</th> <th>Buy Rate</th> <th>Sell Rate</th> </tr> <tr> <?php foreach($datav as $d
foreach
循环数据,但表的设计在动态循环数据之后中断。
我的blade.php代码如下所示:
<table>
<tr>
<th>Name</th>
<th>Buy Rate</th>
<th>Sell Rate</th>
</tr>
<tr>
<?php foreach($datav as $data){ ?>
<td><?php echo $data->name; ?></td>
<td><?php echo $data->buy_rate; ?></td>
<?php } ?>
<?php foreach($sells as $sell){ ?>
<td><?php echo $sell->rate; ?></td>
</tr>
<?php } ?>
</table>
我的控制器代码:
public function test(){
$datav= DB::table('localcurrency')
->where('status',1)
->get();
$sells= DB::table('localcurrency2')
->where('status',1)
->distinct()
->get();
return view('home.home_content')
->with('datav',$datav)
->with('sells',$sells);
}
如何运行此功能?如果将$datav和$sells合并到一个数组中,效果会更好 在上述场景中,首先您的$datav数组将完成,然后它将转到第二个数组 因此,它不会创建一个完整的表。否则,表的对齐方式将错误 Rest取决于在数组中执行的操作
public function test(){
$datav= DB::table('localcurrency')
->where('status',1)
->get();
$sells= DB::table('localcurrency2')
->where('status',1)
->distinct()
->get();
$sells=$sells->toArray();
$results=array();
foreach($datav as $key=>$data)
{
$newarr=array();
$newarr['name']=$data->name;
$newarr['buy_rate']=$data->buy_rate;
$newarr['sell_rate']=$sells[$key]->rate;
$results[]=$newarr;
}
return view('home.home_content')
->with('results',$result);
}
你的观点是这样的
<table>
<tr>
<th>Name</th>
<th>Buy Rate</th>
<th>Sell Rate</th>
</tr>
@foreach($results as $result)
<tr>
<td>{{$result['name']}}</td>
<td>{{$result['buy_rate']}}</td>
<td>{{$result['sell_rate']}}</td>
</tr>
@endforeach
名称
买入价
卖出价
@foreach($results作为$result)
{{$result['name']}
{{$result['buy_rate']}
{{$result['sell_rate']}
@endforeach
- 您最好使用刀片模板引擎语法
@foreach($value作为$value){{{$value}}@endforeach
- 您可以更好地理解希望获得的输出结构
- 在这种情况下,您的任务没有得到解决,因为如果您想填写
一个表一行一行地由一些数据组成,这些数据应该相互连接 一对一的关系,所以你应该在一个周期内完成 - 不能先填充表和的前两列的所有值 然后是另一列的值。表格应逐行填写
放在第二个foreach
之外。您可能需要将阵列合并在一起。请出示您的路线/管制员代码?$datav
和$sales
是阵列还是集合?另外,这是否在.blade.php
文件中?这些数据集是否以某种方式链接?@ross wilson我共享我的路线、控制器和.blade页面代码。为什么不使用@foreach构造?请给我看两个循环的示例代码?这更多是一个注释,然后是一个答案。展示你将如何处理这种情况。现在检查我的最新答案@以前的用户没有提到控制器代码
<table>
<tr>
<th>Name</th>
<th>Buy Rate</th>
<th>Sell Rate</th>
</tr>
@foreach($results as $result)
<tr>
<td>{{$result['name']}}</td>
<td>{{$result['buy_rate']}}</td>
<td>{{$result['sell_rate']}}</td>
</tr>
@endforeach