在Laravel中查询循环内部 [英] Query inside for loop in laravel

查看:147
本文介绍了在Laravel中查询循环内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在LARAVEL控制器的for循环内使用Query时遇到问题。
Im在WHERE条件下循环数据数组,但是代码中似乎有问题,并且什么也不显示,但是当我在for循环中将$ i更改为数字示例(14)时,它将仅显示1数据,但是如果我将它放在顶部,我的for循环将是多余的。

Im having trouble using Query inside the for loop in LARAVEL controller. Im looping an array of data in my WHERE condition but there seems a problem in my code and doesn't show anything but when i change $i in my for loop to a number example (14) it will only show 1 data but my for loop will be redundant if i chane it top a number.

在这里保存我的代码

public函数dtrdata($ name = null)
{

public function 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中包含相同的代码它的工作原理

I WILL ALSO INCLUDE THE SAME CODE in tradtional PHP BUT HERE IT WORKS

<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>


推荐答案

$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);

这篇关于在Laravel中查询循环内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆