我想在一个表中动态显示两个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>
我的路线是:
Route::get('/','WelcomeController@test');
我的控制器代码:
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);
}
我该如何运行?
答案 0 :(得分:2)
最好将$ datav和$ sells合并到一个数组中。
在上述情况下,首先您的$ datav数组将完成,然后转到第二个数组。
因此它不会创建完整的表。否则表对齐将是错误的。
其余部分取决于您在数组中进行的操作。
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
答案 1 :(得分:0)
@foreach($values as $value) {{$value}} @endforeach