在laravel创造出席率

时间:2017-07-20 04:23:55

标签: php mysql laravel

嗯..我为销售员和我的数据库结构创建了一个考勤系统

  1. 用户(id,name)
  2. 出席情况(att_id,user_id,created_at)
  3. 这是我在创建出勤时使用的数据

    $final = array:2 [▼
    0 => array:3 [▼
    "id" => 6
    "name" => "Talha Munshi"
    "attendance" => array:4 [▼
      0 => array:3 [▼
        "attendance" => "P"
        "advance" => "0"
        "date" => Carbon {#250 ▼
          +"date": "2017-07-16 08:07:00.000000"
          +"timezone_type": 3
          +"timezone": "Asia/Karachi"
        }
      ]
      1 => array:3 [▼
        "attendance" => "P"
        "advance" => "0"
        "date" => Carbon {#249 ▼
          +"date": "2017-07-17 08:07:00.000000"
          +"timezone_type": 3
          +"timezone": "Asia/Karachi"
        }
      ]
      2 => array:3 [▼
        "attendance" => "A"
        "advance" => "0"
        "date" => Carbon {#248 ▼
          +"date": "2017-07-18 08:07:00.000000"
          +"timezone_type": 3
          +"timezone": "Asia/Karachi"
        }
       ]
       3 => array:3 [▼
        "attendance" => "L"
        "advance" => "0"
        "date" => Carbon {#241 ▼
          +"date": "2017-07-19 08:07:00.000000"
          +"timezone_type": 3
          +"timezone": "Asia/Karachi"
            }  
          ]
        ]
      ]
      1 => array:3 [▶]
    ]
    
    $days_count = 20; //From first till today (20th)
    

    现在,我尝试了一些编码来填充每天垂直格式的出席情况,并且查看的代码是:

    <table class="table-responsive table-condensed table-striped table-hover table-bordered">
        <thead>
            <tr>
                <td>Salesman</td>
                <?php for($i = 1; $i <= $days_count; $i++){ ?>
                <td><?php echo $i; ?></td>
                <?php }?>
            </tr>
        </thead>
        <tbody>
            @forelse($final as $attend)
                <tr>
                    <td>{{ $attend['name'] }}</td>
                    <?php for($i = 1; $i < $days_count; $i++){
                        $make_date = date("Y-m")."-".$i;
                    ?>
    
                    <?php foreach($attend['attendance'] as $att){ 
                        if($att['date'] == $make_date){
                    ?>
                    <td><?php echo $att['attendance']; ?></td>
                    <?php } else{?> <td>-</td> <?php  } }?>
                    <?php }?>
                </tr>
                @empty
                <tr><td>No Salesman</td></tr>
            @endforelse
        </tbody>
    </table>
    

    但它只是给了答案,实际上压扁了我的大脑。以下是当前结果

    enter image description here

    有人可以帮我解决这个问题吗?

    我通过这个制作了$final数组。

    $users = User::select('id', 'name')
            ->where('type','LIKE','salesman')
            ->get();
    $attendances = Attendance::whereBetween('created_at', [$first_date, $now])
            ->get();
    foreach($users as $user)
        {
            $salesman['id'] = $user->id;
            $salesman['name'] = $user->name;
    
            foreach($attendances as $attendance)
            {
                if($attendance->user_id == $user->id)
                {
                    $attend_2['attendance'] = $attendance->attendance;
                    $attend_2['advance'] = $attendance->advance;
                    $attend_2['date'] = $attendance->created_at;
                    $attend[] = $attend_2;
                }
    
            }
            $salesman['attendance'] = $attend;
            $final[] = $salesman;
        }
    

    必需输出:它应该是什么

    enter image description here

5 个答案:

答案 0 :(得分:1)

第一个问题是这个块

<?php foreach($attend['attendance'] as $att){ 
if($att['date'] == $make_date){
?>
<td><?php echo $att['attendance']; ?></td>
<?php } else{?> <td>-</td> <?php  } }?>
<?php }?>

您要将日期格式Y-m-d与使用$attend_2['date'] = $attendance->created_at;

设置的碳日期进行比较

要解决此问题,您可以在设置数组之前格式化碳日期:$attend_2['date'] = $attendance->created_at->format( 'Y-m-d');

第二个问题是你需要循环几天并且在那个循环中,你循环每个出席。这就是为什么有这么多人物。一个简单的解决方法是在出勤时设置一个标志,然后检查内部循环外的那个标志:

<?php for($i = 1; $i < $days_count; $i++){
    $make_date = date("Y-m")."-".$i;
    $set_attendance_for_day=false;
    foreach($attend['attendance'] as $att){
        if($att['date'] == $make_date){
            $set_attendance_for_day=true;
            ?>
            <td><?php echo $att['attendance']; ?></td>
        <?php } ?>
    <?php } ?>
    <?php if (!$set_attendance_for_day) { ?>
        <td>-</td>
    <?php } ?>
<?php }?>

答案 1 :(得分:1)

我认为您在以下行中的日期比较存在问题。

 if($att['date'] == $make_date){

 }

在这里,$make_date会为您提供类似2017-07-20的日期,并且您尝试与DateTime进行比较2017-07-20 04:55:09这将永远不会相等。

因此,您必须使用Carbon格式化$att['date']

像这样:

if($att['date']->format('Y-m-d') == $make_date){

}

答案 2 :(得分:1)

您可以将视图更改为以下

<table class="table-responsive table-condensed table-striped table-hover table-bordered">
  <thead>
    <tr>
      <td>Salesman</td>
      <?php for($i = 1; $i <= $days_count; $i++){ ?>
      <td>
        <?php echo $i; ?>
      </td>
      <?php }?>
    </tr>
  </thead>
  <tbody>
    @forelse($final as $attend)
    <tr>
      <td>{{ $attend['name'] }}</td>
      <?php 
        for($i = 1; $i < $days_count; $i++){
          $make_date = date("Y-m")."-".$i;
          $set_attendance_for_day=false;
          $attendance_for_day ="-";
          foreach($attend['attendance'] as $att){
            if($att['date'] == $make_date){
               $attendance_for_day = "P";
            } 
          } 
      ?>
      <td>
        <?php echo $attendance_for_day; ?>
      </td>

      <?php }?>
    </tr>
    @empty
    <tr>
      <td>No Salesman</td>
    </tr>
    @endforelse
  </tbody>
</table>

**根据评论更新

<table class="table-responsive table-condensed table-striped table-hover table-bordered">
  <thead>
    <tr>
      <td>Salesman</td>
      <?php for($i = 1; $i <= $days_count; $i++){ ?>
      <td>
        <?php echo $i; ?>
      </td>
      <?php }?>
    </tr>
  </thead>
  <tbody>
    @forelse($final as $attend)
    <tr>
      <td>{{ $attend['name'] }}</td>
      <?php 
        for($i = 1; $i < $days_count; $i++){
          $make_date = date("Y-m")."-".$i;
          $set_attendance_for_day=false;
          $attendance_for_day ="-";
          foreach($attend['attendance'] as $att){
            if($att['date'] == $make_date){
               $attendance_for_day = "P";
            } 
          } 
      ?>
      <td>
        <?php echo $attendance_for_day; ?>
        <?php if($i==20){ ?>

          ---------------------------------------------------
          <PLACE YOUR FORM HERE>
          ---------------------------------------------------
        <?php } ?>
      </td>

      <?php }?>
    </tr>
    @empty
    <tr>
      <td>No Salesman</td>
    </tr>
    @endforelse
  </tbody>
</table>

答案 3 :(得分:1)

这将搜索出席日期是否存在并相应打印

@forelse($final as $attend)
<tr>
    <td>{{ $attend['name'] }}</td>
    @for ($i = 1; $i < $days_count; $i++)
    {{--*/ $make_date = Carbon\Carbon::create(date("Y-m")."-".$i); /*--}}
    {{--*/ $key = array_search($make_date, array_column($attend['attendance'], 'date')); /*--}}
    @if ($key !== FALSE)
        <td>{{ $att['attendance'][$key]['attendance'] }}</td>
    @else
        <td>-</td>
    @endif
    @endfor
</tr>
@empty
<tr><td>No Salesman</td></tr>
@endforelse

答案 4 :(得分:1)

试试这个。删除额外的循环。创建标志变量并检查它。

d.get('a', {}).get('j')