如何使用laravel以正确的顺序显示2个月的值

时间:2015-08-08 07:23:44

标签: laravel-4

这是我的表wys_attendence:

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`

我如何修改我的查询以实现上述结果以及检查所有日期(2015年7月1日至2015年8月31日)是否在数据库中,如果在数据库中则只显示一次并显示所有日期来自数据库的详细信息?

1 个答案:

答案 0 :(得分:0)

如果您想按日期对记录进行分组,请按照http://laravel.com/docs/4.2/queries#selects

中的说明使用groupBy()
$attendance = DB::table('wys_teacherattendances')
->where('t_amonth', $amonth)
->where('t_ayear',$ayear)
->groupBy('t_adate')
->get();