如何在 Laravel 中 运行 一个 html table 中的两个 foreach 循环?
How to run two foreach loop inside one html table in Laravel?
我想在一个 table 中动态显示两个 foreach
循环数据,但是 table 的设计在动态循环数据后中断。
我的 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 数组将完成,然后转到第二个数组。
所以不会创建完整的table。否则 table 对齐将是错误的。
其余取决于您在数组中进行的操作。
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
- 你最好使用 Blade 模板引擎语法
@foreach($values as $value) {{$value}} @endforeach
- 你最好了解你想要得到什么样的输出结构
- 在这种情况下你的任务没有解决,因为如果你想填补
一个table一行一行的数据,这个数据应该是互联的
一对一的关系,所以你应该在一个周期内完成
- 您不能先填写 table 和前两列的每个值
然后是另一列的值。 Table 应逐行填写
我想在一个 table 中动态显示两个 foreach
循环数据,但是 table 的设计在动态循环数据后中断。
我的 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 数组将完成,然后转到第二个数组。
所以不会创建完整的table。否则 table 对齐将是错误的。
其余取决于您在数组中进行的操作。
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
- 你最好使用 Blade 模板引擎语法
@foreach($values as $value) {{$value}} @endforeach
- 你最好了解你想要得到什么样的输出结构
- 在这种情况下你的任务没有解决,因为如果你想填补
一个table一行一行的数据,这个数据应该是互联的 一对一的关系,所以你应该在一个周期内完成 - 您不能先填写 table 和前两列的每个值 然后是另一列的值。 Table 应逐行填写