2015-08-08 60 views
1

在正確的2個階月份值這是我的表wys_attendence:如何顯示使用laravel

id studid adate amonth ayear acls_id attendence  
1 28  02  07  2015 10  1  
2 31  02  07  2015 10  0 
4 32  02  07  2015 10  1 
5 28  30  07  2015 10  0 
6 31  30  07  2015 10  1 
7 32  30  07  2015 10  1 
9 28  31  07  2015 10  1 
10 31  31  07  2015 10  1 
11 32  31  07  2015 10  1 
13 28  06  08  2015 10  1 
14 31  06  08  2015 10  0 
15 32  06  08  2015 10  1 
17 28  07  08  2015 10  0 
18 31  07  08  2015 10  1 
19 32  07  08  2015 10  1 
21 28  08  08  2015 10  1 
22 31  08  08  2015 10  1 
23 32  08  08  2015 10  0 
24 28  12  08  2015 10  1 
25 31  12  08  2015 10  1 
26 32  12  08  2015 10  0 

我要檢查,如果t_adates之間的值是在表中,並從t_adates

之間的顯示值

如果我選擇1/07/2015 to 31/08/0215

我得到的輸出不正確。這就是我得到:

studid 2/07/2015 06/08/2015 07/08/2015 08/08/2015 30/07/205 31/07/2015 
    28  1    1   0   1   0   1   
    31  0    0   1   1   1   1   
    32  1    1   1   0   1   1   

不顯示值是不正確的順序

但我想它是這樣的:

studid 2/07/2015 30/07/205 31/07/2015 06/08/2015 07/08/2015 08/08/2015 
    28  1   0   1   1   0   1 
    31  0   1   1   0   1   1 
    32  1   1   1   1   1   0 

我的控制器代碼是在這裏

 `$startdate_exploded = explode("/",Input::get('curdate'));   
     $enddate_exploded = explode("/",Input::get('enddate'));     
     $attendence_tbl = WysAttendancename::where('cls_id',$id)->first(); 
     $wys_attendence_table = $attendence_tbl->attendance_name; 
     $attendance = DB::table($wys_attendence_table) 
        ->whereBetween('adate', array($startdate_exploded[0], $enddate_exploded[0])) 
        ->whereBetween('amonth', array($startdate_exploded[1], $enddate_exploded[1])) 
        ->whereBetween('ayear', array($startdate_exploded[2], $enddate_exploded[2])) 
        ->groupBy('adate') 
        ->get(); 
     $stud_attend = DB::table($wys_attendence_table) 
        ->whereBetween('adate', array($startdate_exploded[0], $enddate_exploded[0])) 
        ->whereBetween('amonth', array($startdate_exploded[1], $enddate_exploded[1])) 
        ->whereBetween('ayear', array($startdate_exploded[2], $enddate_exploded[2]))  
        ->get();` 

我view.blade.php是

`<td>Student Name</td> 
        @foreach($attendance as $attendances) 
        <td><font size="-1">{{$attendances->adate}}-{{$attendances->amonth}}-{{$attendances->ayear}}</font></td> 
        @endforeach 
        </tr> 
        @foreach($students as $student) 
        @if($student->studcls == $id) 
        <tr> 
        <td>{{$student->studname}}</td> 
        @foreach($stud_attend as $stud_attends) 
        @if($student->id == $stud_attends->studid) 
        @if($stud_attends->attendence == 1) 
        <td><font color="green" size="3">p</font></td> 
        @elseif($stud_attends->attendence == 0) 
        <td><font color="red" size="3">a</font></td> 
        @endif 
        @endif 
        @endforeach 
        </tr> 
        @endif 
        @endforeach` 

如何修改我的查詢以實現上述結果以及檢查數據庫中的所有日期(01/07/2015至31/08/2015)是否在數據庫中,然後在日期中僅顯示一次並顯示數據庫中的所有細節?

回答

0

如果按日期要組記錄,然後使用GROUPBY()作爲http://laravel.com/docs/4.2/queries#selects

$attendance = DB::table('wys_teacherattendances') 
->where('t_amonth', $amonth) 
->where('t_ayear',$ayear) 
->groupBy('t_adate') 
->get(); 
解釋