php - Unable to loop through values using for - Laravel 5.6 Blade Template
问题描述
The value of the for loop is stuck at 1.
I am trying to run this loop to print the name of the event at the top of the modal box. But the same name appears each time.
I've given continue statement and tried too. Yet the value of $i is stuck at 1
It would be of great help if you could find a solution for this
Thanks in advance.
<?php $noofevents = DB::table('events')->count(); ?>
@for($i = 1; $i <= $noofevents; $i++)
<?php $current_event_name = DB::table('events')->where('id',$i)->value('Event_Name'); ?>
<div id="sponsor-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="display-4" style = "text-align : center; font-size : 30px;">Sponsors for {{$current_event_name}}</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-content">
<img src ="{{asset('images/Sponsors/paytm.jpg')}}" alt ="" width = "200px" class = "mx-auto d-block">
</div>
<div class="card-footer">
<p class = "text-center"></p>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
@endfor
解决方案
You need to differentiate modal box with different ids
Please Use this part in your controller and pass it to your blade view using compact.
<?php $noofevents = DB::table('events')->count(); ?>
Use this part in blade :
@for($i = 1; $i <= $noofevents; $i++)
<?php $current_event_name = DB::table('events')->where('id',$i)->value('Event_Name'); ?>
<div id="sponsor-modal{{ $i }}" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="display-4" style = "text-align : center; font-size : 30px;">Sponsors for {{$current_event_name}}</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-content">
<img src ="{{asset('images/Sponsors/paytm.jpg')}}" alt ="" width = "200px" class = "mx-auto d-block">
</div>
<div class="card-footer">
<p class = "text-center"></p>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
@endfor
So now you have different modal boxes for total noofevents. you need to call this modal boxes with ids appended by $i.
推荐阅读
- node.js - Angular 8 Express Post 引发 COR 错误
- sql-server - 删除未使用的数据库后如何减小 SQL Server docker 卷的大小?
- java - 适用于 iOS 和 Android 的 uGrockIT RFID SDK
- android - android - 如何将操作栏大小放在尺寸资源文件中
- javascript - 如何自动导入默认的 React 类
- c# - 使用圆弧半径和旋转在两点之间创建圆弧
- intellij-idea - 如何更改 Intellij 的 GIT 注释列的背景颜色?
- powershell - 提取 7zip 脚本在 Windows Server 2019 任务计划程序中不起作用
- ruby-on-rails - Book.all 类和一系列书籍之间的区别
- scala - 错误:类型推断无法确定类型。升级scala版本时出错