[英]How to run two foreach loop inside one html table in Laravel?
我想在一个表中动态显示两个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);
}
我该怎么办呢?
如果在一个数组中合并$ 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
@foreach($values as $value) {{$value}} @endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.