2016-04-20 61 views
0

我想在我的項目中顯示機器維護表,目前我在單個表中顯示所有數據,但現在我想以明智的方式顯示錶。查詢的如何在日期表中顯示mysql數據

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

輸出=

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; 
      } 

目前我的桌子的樣子..

我想看起來像。

請幫

+0

這似乎只是你需要做一個小的調整你的循環 – Strawberry

回答

1

你在你的代碼錯誤...你有「==」來檢查if語句。此外,in_array只返回一個布爾值。你不能把它與非布爾值比較

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

嗨Shimu,in_array有一些錯誤,in_array應該是2參數。 – aniruddh

+0

對不起,你是對的......但是,如果密鑰存在的話,你想切磋?那麼你應該使用函數array_key_exists。我編輯我的答案 – Shimu

+0

它的工作很好,最終我得到了我的結果。謝謝 – aniruddh

0

你可以簡單地使用這樣的PHP代碼做到這一點:

僞代碼:

$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 
    } 
    } 
} 
} 
+0

喜追兇,謝謝,但這些代碼不會產生精確的結果,我很困惑如何使用它們 – aniruddh

+0

將實際代碼粘貼到您的問題中。我可以幫你。 – Pupil

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,您可以按照您的行值傳遞。

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; 
    } 
} 
+0

嗨Brijal,其顯示空白陣列 – aniruddh

+0

我已更新代碼@aniruddh –