我用循环来获取查询并能够存储在数组中。数组例如:看起来像我:
DD($ S);
array:2 [▼
"Block A" => array:49 [▶]
"Block K" => array:149 [▶]
]
现在有了这些数组,我有更多的数据,我想在刀片的选择框中显示它。
array:2 [▼
"Block A" => array:49 [▼
0 => array:1 [▼
"Block A0" => "1"
]
我想只显示1在Block A0 => 1的位置
homeController.php
foreach ($v as $f ) {
$m[$i] = [
$l.$i => $f->rNum,
];
$s[$l] = $m;
$i++;
}
$s = (array_filter($s));
return view('home')->with($s);
// I have also tried with
//return view('home')->with('res',$s);
Home.blade.php
@foreach ($res as $s)
<option value="> {{_____?____}}" </option>
//I only want to display 1 to 49 here
@endforeach
请记住,有2个阵列,块A和块K.
所以选择框应该具有块A的所有值(即:1到49)和第二个选择框中块K的所有值(即:1到149)。
我不太清楚如何获得这里的值,但至少我能够获得控制器中的值
**
完整的控制器代码
public function viewHall($id){
$path = request()->path();
$substring = substr($path, 0,10);
$title;
$viewApp;
$viewroom = "";
$s[] = array();
$i = 1;
if ($substring == "view/hall/"){
$viewApp = DB::select('Select * from applications where hall = "'.$id .'" AND status = "pending"');
foreach ($viewApp as $k) {
$a[] = $k->flat;
}
$countA = (count($a));
foreach ($a as $l) {
$viewroom = DB::select('Select applications.* , residences.* , rooms.* from applications
LEFT Join residences on applications.resi = residences.resiName
LEFT JOIN rooms on rooms.resiID = residences.id
where hall = "'.$id .'" AND status = "pending" AND rooms.available = 0
AND rooms.flatName ="'.$l.'" group by rooms.flatName, rooms.roomNum');
foreach ($viewroom as $f ) {
$s[$l][$i] = [
$l => $f->roomNum,
];
$i++;
}
}
$s = (array_filter($s));
$title = "Hall (" . $id .")";
return view('admin.viewapp')->with('applications', $viewApp)->with('title',$title)->with('rid',$id)->with('rooms',$viewroom)
->with('roomNs', $s)->with('flat',$a)->with('flatc', $m);
}
elseif ($substring == "view/resi/"){
$viewApp = DB::select('Select * from applications where resi = "'.$id .'" AND status = "pending"');
$viewroom = DB::select('Select applications.* , residences.* , rooms.* from applications
LEFT Join residences on applications.resi = residences.resiName
LEFT JOIN rooms on rooms.resiID = residences.id
where resi = "'.$id .'" AND status = "pending" AND rooms.available = 0');
$title = "Residence (" . $id .")";
}
elseif ($substring == "view/year/"){
$viewApp = DB::select('Select * from applications where year = "'.$id .'" AND status = "pending"');
$viewroom = DB::select('Select applications.* , residences.* , rooms.* from applications
LEFT Join residences on applications.resi = residences.resiName
LEFT JOIN rooms on rooms.resiID = residences.id
where applications.year = "'.$id .'" AND status = "pending" AND rooms.available = 0');
$title = "Year (" . $id .")";
}
else{
$viewApp = DB::select('Select * from applications where status = "pending"');
$viewroom = DB::select('Select applications.* , residences.* , rooms.* from applications
LEFT Join residences on applications.resi = residences.resiName
LEFT JOIN rooms on rooms.resiID = residences.id
where status = "pending" AND rooms.available = 0');
$title = "View All";
}
return view('admin.viewapp')->with('applications', $viewApp)->with('title',$title)->with('rid',$id)->with('rooms',$viewroom)
-with('roomNs', $roomN);
}
**
**
View.blade.php
<form class="" action="{{URL('process/')}}" method="post">
{{ csrf_field() }}
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Hall</th>
<th scope="col">Residence</th>
<th scope="col">Year</th>
<th scope="col">Flat</th>
<th scope="col">Room</th>
<th scope="col">Status</th>
<th scope="col">Submitted on</th>
<th scope="col">Approve</th>
<th scope="col">Reject</th>
</tr>
</thead>
<tbody>
<tr>
@foreach ($applications as $show)
<input type="hidden" name="id" value="{{$show->id}}">
<input type="hidden" name="hall" value="{{$show->hall}}">
<input type="hidden" name="resID" value="{{$show->resID}}">
<input type="hidden" name="year" value="{{$show->year}}">
<input type="hidden" name="flat" value="{{$show->flat}}">
<th scope="row">{{$i}}</th>
<td>{{$show->hall}}</td>
<td>{{$show->resi}}</td>
<td>{{$show->year}}</td>
<td>{{$show->flat}}</td>
<td>
<div class="form-group">
<select class="form-control" id="roomNo" name="roomNo" required autofocus>
<option value="" selected>Select Room Number</option>
<?php $i=0; ?>
@foreach ($roomNs as $key1 => $value1)
@foreach ($value1 as $key2 => $value2)
@foreach ($value2 as $key3 => $value3)
<option value="{{ $value3 }}"> {{ $value3 }} </option>
@endforeach
@endforeach
@endforeach
</select>
</div>
</td>
<td>{{$show->status}}</td>
<td>{{$show->created_at}}</td>
<td><input type="submit" class="btn btn-primary"name="approve" value="Approve"></td>
<td><input type="submit" class="btn btn-danger"name="reject" value="Reject"></td>
</tr>
@endforeach
</tbody>
</table>
</form>
**
所以,因为那是你的数组结构,你要做的就是使用3个foreach循环。
假设在你的控制器中返回:
return view('home')->with('res', $s);
以下是您希望在视图中执行的操作:
@foreach ($res as $key1 => $value1)
@foreach ($value1 as $key2 => $value2)
@foreach ($value2 as $key3 => $value3)
<option value="{{ $value3 }}"> {{ $key3 }}" </option>
@endforeach
@endforeach
@endforeach
PS
请正确命名您的变量。不要像$s
或$res
那样命名,如果你适当地命名变量,它将在你的项目增长时帮助你很多。
跟进回答:
在你的控制器中。我不确定$v
变量的值是什么,以及你想在foreach循环中做什么。我想解决你为“Block K”而不是149获取100个数据的问题,你必须这样做:
foreach ($v as $f ) {
$s[$l][$i] = [
$l.$i => $f->rNum,
];
$i++;
}
答案#2:
这是我在您的观点中推荐的内容:
<form class="" action="{{URL('process/')}}" method="post">
{{ csrf_field() }}
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Hall</th>
<th scope="col">Residence</th>
<th scope="col">Year</th>
<th scope="col">Flat</th>
<th scope="col">Room</th>
<th scope="col">Status</th>
<th scope="col">Submitted on</th>
<th scope="col">Approve</th>
<th scope="col">Reject</th>
</tr>
</thead>
<tbody>
@foreach ($applications as $show)
<tr>
<input type="hidden" name="id" value="{{$show->id}}">
<input type="hidden" name="hall" value="{{$show->hall}}">
<input type="hidden" name="resID" value="{{$show->resID}}">
<input type="hidden" name="year" value="{{$show->year}}">
<input type="hidden" name="flat" value="{{$show->flat}}">
<th scope="row">{{$i}}</th>
<td>{{$show->hall}}</td>
<td>{{$show->resi}}</td>
<td>{{$show->year}}</td>
<td>{{$show->flat}}</td>
<td>
<div class="form-group">
<select class="form-control" id="roomNo" name="roomNo" required autofocus>
<option value="" selected>Select Room Number</option>
<?php $i=0; ?>
@foreach ($roomNs[$show->flat] as $key1 => $value1)
@foreach ($value1 as $key2 => $value2)
<option value="{{ $value2 }}"> {{ $value2 }} </option>
@endforeach
@endforeach
</select>
</div>
</td>
<td>{{$show->status}}</td>
<td>{{$show->created_at}}</td>
<td><input type="submit" class="btn btn-primary"name="approve" value="Approve"></td>
<td><input type="submit" class="btn btn-danger"name="reject" value="Reject"></td>
</tr>
@endforeach
</tbody>
</table>
</form>
大段引用