在LARAVEL控制器的for循环内使用Query时遇到问题。 我在WHERE条件下循环了一个数据数组,但是代码中似乎有问题,什么也没显示,但是当我在for循环中将$ i更改为数字示例(14)时,它只会显示1个数据,但是我的for如果我将其放在最前面,循环将是多余的。
在这里输入我的代码
公共函数dtrdata($ name = null) {
$d=cal_days_in_month(CAL_GREGORIAN,12,2017);// get days of the month
$totalnumdays = $d; //31 days for dec 2017
$firstdaynum = 4; //6 max // friday is my first day of the month for dec 2017
$reset = 0;
$weekdays = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
for($i=1;$i<=$totalnumdays;$i++){
echo "<table><tr><td>". $i ."/";
for($x=0; $x<count($weekdays); $x++){
$index = $firstdaynum+$reset; //fri start; index in array
echo $weekdays[$index] ."</td></tr></table>";
if($index == 6){
$reset = $firstdaynum * -1;
}else{
$reset++;
}
break;
}
$xy='15';
$dtr= DB::table('dtrrecords')
->join('employees', 'dtrrecords.bio_id', '=', 'employees.bio_id')
->select(DB::raw("dtrrecords.bio_id as bio_id, employees.time_code as time_code, week_day, dtrrecords.month as month, dtrrecords.year as year, dtrrecords.date_only as date_only,
employees.employee_lname as employee_lname,
employees.employee_fname as employee_fname,
employees.employee_mname as employee_mname,
STR_TO_DATE(date_only, '%m/%d/%Y') AS date_only_converted,
MAX(CASE WHEN ampm_type = 'AM IN' THEN time_only END) AS AM_IN,
MAX(CASE WHEN ampm_type = 'AM OUT' THEN time_only END) AS AM_OUT,
MAX(CASE WHEN ampm_type = 'PM IN' THEN time_only END) AS PM_IN,
MAX(CASE WHEN ampm_type = 'PM OUT' THEN time_only END) AS PM_OUT")
)->where('dtrrecords.bio_id', $name)
->where('month', '12')
->where('year', '2017')
->where(DB::raw('DAY(STR_TO_DATE(date_only, "%m/%d/%Y"))'), '=', $i) //$i here seems to have the problem
->groupBy('dtrrecords.bio_id','date_only_converted')->get();
}
return view('pages/admin.dtrdata', compact('name','employee','dtr','i'));
}
我还将在传统的PHP中包含相同的代码,但在这里可以工作
<table class="table table-bordered">
<thead>
<tr>
<th>Time Code</th>
<th>IN</th>
<th>OUT</th>
<th>IN</th>
<th>OUT</th>
</tr>
</thead>
<tbody>
<?php
/*$d=cal_days_in_month(CAL_GREGORIAN,12,2017);// from 4=friday 0=mon*/
$totalnumdays = 31; //31
$firstdaynum = 4; //6 max // convert day to num
$reset = 0;
$weekdays = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
for($i=1;$i<=$totalnumdays;$i++){
echo "<tr><td>". $i ."/";
for($x=0; $x<count($weekdays); $x++){
$index = $firstdaynum+$reset; //fri start; index in array
echo $weekdays[$index] ."</td>";
if($index == 6){
$reset = $firstdaynum * -1;
}else{
$reset++;
}
break;
}
$sql = "SELECT
time_code,
bio_id,
STR_TO_DATE(date_only, '%m/%d/%Y') AS date_only_converted,
MAX(CASE WHEN ampm_type = 'AM IN' THEN time_only END) AS AM_IN,
MAX(CASE WHEN ampm_type = 'AM OUT' THEN time_only END) AS AM_OUT,
MAX(CASE WHEN ampm_type = 'PM IN' THEN time_only END) AS PM_IN,
MAX(CASE WHEN ampm_type = 'PM OUT' THEN time_only END) AS PM_OUT,
week_day FROM dtrrecords NATURAL JOIN employees where bio_id='10258' and month = '12'
and year = '2017' and DAY(STR_TO_DATE(date_only, '%m/%d/%Y')) = ". $i ."
GROUP BY bio_id , date_only_converted";
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($query)) {
$amins=$row['AM_IN'];
$amouts=$row['AM_OUT'];
$pmins=$row['PM_IN'];
$pmouts=$row['PM_OUT'];
}
//if($amins != '' && $amins != null){
echo "<td>".$amins."</td>";
//}else{
// echo "<td></td>"; //display am_in row here
//}
echo "<td>" .$amouts."</td>"; //display am_out row here
echo "<td>" .$pmins."</td>"; //display pm_in row here
echo "<td>" .$pmouts."</td>"; //display pm_out row here
echo "</tr>";
$amins = '';
$amouts = '';
$pmins = '';
$pmouts = '';
}
?>
</tbody>
</table>
答案 0 :(得分:0)
$user_selectedgrhead = DB::table('tablename')->where('subscriber_id',$user_id)->get()->pluck('fieldyouneed');
echo $count = count($user_selectedgrhead);
$complete_list = array();
for($i=0;$i<$count; $i++){
$complete_list[] = $user_selectedgrhead[$i];
}
print_r($complete_list);