如何在Laravel中显示原始总数

时间:2015-08-10 11:36:58

标签: 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  

如何检查每个学生的总和,其中amonth = 07,ayear = 2015年,注意力= 1

我的输出不正确

RollNo StudentName 02 13 14 15 16 17 20 Total PresentsTotal Absents

28   Gokul        p  p   p  p   a   p   p       5               2

31   Goku         p p   p   a   p   p   p       5               2

32   Gok          p p   p   p   p   a   a       5               2

我得到了最后一个学生的值关注度= 1且注意力= 0

如何让所有学生计算价值关注度= 1,并且注意力= 0次。

我想要这样

RollNo StudentName 02 13 14 15 16 17 20 Total Presents TotalAbsents

28   Gokul        p  p   p  p   a   p   p       6                 1

31   Goku         p p   p   a   p   p   p       6                 1

32   Gok          p p   p   p   p   a   a       5                 2

我的controller.php

` $attendence_tbl = WysAttendancename::where('cls_id',$id)->first();
        $wys_attendence_table = $attendence_tbl->attendance_name;
        $attendance = DB::table($wys_attendence_table)
                    ->where('amonth',$amonth)
                    ->where('ayear',$ayear)
                    ->groupBy('adate')
                    ->get();
        $stud_attend = DB::table($wys_attendence_table)
                    ->where('amonth',$amonth)
                    ->where('ayear',$ayear)
                    ->get();
        foreach($students as $student){
        $cunt1 = DB::table($wys_attendence_table)
                 ->where('studid',$student->id)
                 ->where('amonth',$amonth)
                 ->where('ayear',$ayear) 
                 ->where('attendence',1)
                 ->count();
            $cunt0 = DB::table($wys_attendence_table)
                 ->where('studid',$student->id)
                 ->where('amonth',$amonth)
                 ->where('ayear',$ayear) 
                 ->where('attendence',0)
                 ->count();            
        }`

我的view.blade.php

                 `@foreach($students as $student)                  
                  @if($student->studcls == $id)
                  <tr> <td>{{$student->id}}</td>
                    <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
                  <td>{{$cunt1}}</td> 
                <td>{{$cunt0}}</td> 
                  </tr>          
                  @endif                 
                  @endforeach` 

如何解决我的问题?

我尝试回显method..var_dump($ cunt1)我得到了正确答案int(6)int(6)int(5)和var_dump($ cunt0)是int(1)int(1)int(2)

但是在view.blade.php上运行我得到5 5 5和2 2 2所有studid的最后一个值。

如何修改我的view.blade.php代码?

1 个答案:

答案 0 :(得分:0)

    $attendance = DB::table($wys_attendence_table)
            ->whereRaw('amonth = "'.$amonth.'"')
            ->whereRaw('ayear = "'.$ayear.'"')
            ->groupBy('adate')
            ->get();

    $presentsCountQuery = DB::table($wys_attendence_table)
            ->select(DB::raw('studid, COUNT(*) AS presents_counter'))
            ->whereRaw('amonth = "'.$amonth.'"')
            ->whereRaw('ayear = "'.$ayear.'"')
            ->whereRaw('attendence = 1')
            ->groupBy('studid')
            ->toSql();

    $absentsCountQuery = DB::table($wys_attendence_table)
            ->select(DB::raw('studid, COUNT(*) AS absents_counter'))
            ->whereRaw('amonth = "'.$amonth.'"')
            ->whereRaw('ayear = "'.$ayear.'"')
            ->whereRaw('attendence = 0')
            ->groupBy('studid')
            ->toSql();

    $stud_attend = DB::table($wys_attendence_table . ' as table')
            ->leftJoin(DB::raw('(' . $presentsCountQuery . ') AS presents_counter_table'), 'table.studid', '=', 'presents_counter_table.studid')
            ->leftJoin(DB::raw('(' . $absentsCountQuery . ') AS absents_counter_table'), 'table.studid', '=', 'absents_counter_table.studid')
            ->where('table.amonth',$amonth)
            ->where('table.ayear',$ayear)
            ->groupBy('table.studid')
            ->get();
    //var_dump($stud_attend);

查看:

@foreach($students as $student)
    @if($student->studcls == $id)
        <tr>
            <td>{{$student->id}}</td>
            <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
                    <td>{{$stud_attends->presents_counter}}</td>
                    <td>{{$stud_attends->absents_counter}}</td>
                @endif
            @endforeach
        </tr>
    @endif
@endforeach