如何使用datewise在Table中显示mysql数据

时间:2016-04-20 06:43:45

标签: php mysql

我想在我的项目中显示机器维护表,目前我在单个表中显示所有数据,但现在我想在日期显示表。

SELECT * ,DATE_FORMAT(date,'%Y-%m-%d') as groupdate,SUM(operationtime) as usetime  FROM `logusages` WHERE machineid = '$id' GROUP BY groupdate,empCode"

Query =

的输出
Array
(
    [workingLogs] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 13
                    [key] => 5BTIB4EoklT6dVfTUR4ezwb8T3xdeTAi
                    [machineid] => HIT232300
                    [empCode] => 1003
                    [employeeid] => Jon Doe
                    [date] => 2016-03-21 09:27:33
                    [time] => 11:22:00
                    [operationtime] => 1800
                    [groupdate] => 2016-03-21
                    [usetime] => 1800
                )

            [1] => stdClass Object
                (
                    [id] => 14
                    [key] => 5BTIB4EoklT6dVfTUR4ezwb8T3xdeTAi
                    [machineid] => HIT232300
                    [empCode] => 1003
                    [employeeid] => Jon Doe
                    [date] => 2016-03-22 09:27:33
                    [time] => 11:22:00
                    [operationtime] => 1800
                    [groupdate] => 2016-03-22
                    [usetime] => 1800
                )

            [2] => stdClass Object
                (
                    [id] => 8
                    [key] => 5BTIB4EoklT6dVfTUR4ezwb8T3xdeTAi
                    [machineid] => HIT232300
                    [empCode] => 1004
                    [employeeid] => Jon Doe
                    [date] => 2016-03-22 09:27:33
                    [time] => 11:22:00
                    [operationtime] => 1800
                    [groupdate] => 2016-03-22
                    [usetime] => 1800
                )

            [3] => stdClass Object
                (
                    [id] => 15
                    [key] => 5BTIB4EoklT6dVfTUR4ezwb8T3xdeTAi
                    [machineid] => HIT232300
                    [empCode] => 1003
                    [employeeid] => Jon Doe
                    [date] => 2016-03-23 09:27:33
                    [time] => 11:22:00
                    [operationtime] => 1800
                    [groupdate] => 2016-03-23
                    [usetime] => 3600
                )

            [4] => stdClass Object
                (
                    [id] => 9
                    [key] => 5BTIB4EoklT6dVfTUR4ezwb8T3xdeTAi
                    [machineid] => HIT232300
                    [empCode] => 1004
                    [employeeid] => Jon Doe
                    [date] => 2016-03-23 09:27:33
                    [time] => 11:22:00
                    [operationtime] => 1800
                    [groupdate] => 2016-03-23
                    [usetime] => 3600
                )

            [5] => stdClass Object
                (
                    [id] => 12
                    [key] => 5BTIB4EoklT6dVfTUR4ezwb8T3xdeTAi
                    [machineid] => HIT232300
                    [empCode] => 1003
                    [employeeid] => Jon Doe
                    [date] => 2016-03-25 09:27:33
                    [time] => 11:22:00
                    [operationtime] => 1800
                    [groupdate] => 2016-03-25
                    [usetime] => 1800
                )

            [6] => stdClass Object
                (
                    [id] => 11
                    [key] => 5BTIB4EoklT6dVfTUR4ezwb8T3xdeTAi
                    [machineid] => HIT232300
                    [empCode] => 1004
                    [employeeid] => Jon Doe
                    [date] => 2016-03-25 09:27:33
                    [time] => 11:22:00
                    [operationtime] => 1800
                    [groupdate] => 2016-03-25
                    [usetime] => 1800
                )

        )

)

我尝试使用in_array处理此代码,但该代码无效

  foreach ($workingLogs as $key => $date) {
          if(in_array($date['groupdate']) = $date['groupdate']){
            $datewise[$date['groupdate']]=$date;
          }else{
            $datewise[]=$date;
          }

目前我的桌子看起来像..

我想看起来像。

请帮忙

4 个答案:

答案 0 :(得分:1)

您的代码中有错误...您必须使用“==”检查if语句。此外,in_array只返回一个布尔值。您无法将其与非布尔值进行比较

 foreach ($workingLogs as $key => $date) {
   if(array_key_exists('groupdate', $date) ){
     $datewise[$date['groupdate']]=$date;
   }else{
     $datewise[]=$date;
   }
}

答案 1 :(得分:0)

你可以简单地使用这样的PHP代码:

PSUEDO代码:

$arr = array();
while ($row = fetch results) {
 $elem = array(); 
 $elem['id'] = $row->id;
 $elem['key'] = $row->key;
 $elem['machineid'] = $row->machineid;
 $elem['empCode'] = $row->empCode;
 $elem['employeeid'] = $row->employeeid;
 $elem['date'] = $row->date;
 $elem['time'] = $row->time;
 $elem['operationtime'] = $row->operationtime;
 $elem['groupdate'] = $row->groupdate;
 $elem['usetime'] = $row->usetime;
 $arr['groupdate'][] = $elem;
}

在显示结果时,使用groupdate作为键和其他字段,如下所示:

if (! empty($arr)) {
 foreach ($arr as $groupdate => $elem) {
   echo $groupdate;
   if (! empty($elem)) {
    foreach ($elem as $field => $val) {
      // Show records under this groupdate
    }
  }
 }
}

答案 2 :(得分:0)

<?php
include_once('connect.php');
{
    error_reporting( error_reporting() & ~E_NOTICE ); 
                $querys="select date from logusages";
                $result=mysqli_query($con,$querys);
   while(($row = mysqli_fetch_assoc($result))){
             $cart = $row;
 }      
        // mysqli_free_result($result);
}
     $select="select * from logusages";
     $results = mysqli_query($con, $select);
    if(mysqli_num_rows ($results) > 0)
    { 
        $cart = $row;
      while(list($id,$name,$date) = mysqli_fetch_array($results, MYSQLI_NUM))
       {
                  $j=1;   
            $cart[$date][] = array(
           'id' => $id, 
           'name' => $name);
            $j++;
       }
       echo json_encode($cart). "\n";
    }      
       ?>

我刚刚在日期内传递了id和name,你可以根据行值传递。

答案 3 :(得分:0)

使用此

$newArray = array();
foreach ($workingLogs as $key => $date) 
{

    $datetime = date("Y-m-d",$date['groupdate']);

    if(isset($newArray[$datetime]))
    {
        $newArray[$datetime][] = $date; 
    }   
    else
    {
        $newArray[$datetime] = $date;   
    }
}