2014-01-19 102 views
0

首先讓我試着解釋我正在嘗試做什麼。在不知道密鑰的情況下對多維數組執行if語句

我有一個存儲在數據庫中的事件的日曆,每個事件都是一個工作日。我保存在數據庫中的重要內容是用戶,日期,一年的周(1-52)和工作時間。現在我想通過增加了通過分組在1周,2,3工作時間的工作時間,...,51和52我到目前爲止的代碼來計算工資是:

<?php 
include ('require.php'); 
include ('dblink.php'); 
$time =[]; 
$results = mysqli_query($con,"SELECT * FROM events WHERE user = '".$_SESSION['s_username']."'") or die(mysqli_error()); 
if (mysqli_num_rows($results) > 0){ 
    while($row = mysqli_fetch_assoc($results)){ 
      if($row['week'] % 2 == 0){ 
      $week = $row['week'] - 1; // if its an even week then -1 to make it an odd week to add up the hours of the two weeks together. 
     }else{ 
      $week = $row['week']; 
     } 
     if (isset($time['week']) && $time['week'] == $week){ 
      $time['hours'] += $row['hours']; //If the previous week was added then add the second week to it. 
     }else{ 
      $year = explode("-", $row['date']); 
      $time[] = array('week' => $week, 'hours' => $row['hours'], 'year' => $year[0]); 
     } 
    } 
} 
foreach($time as $indTime){ 
    echo $indTime['hours'].' hours were spent on the '.$indTime['week'].' week of '.$indTime['year'] . '<br />'; 
} 
?> 

問題出現在第13行,我有一個if聲明。我知道這是不正確的,因爲我的數組是多維的,並且沒有定義$time['week']。但我不知道鑰匙,所以我不能這麼做,比如說,$time[23]['week']

要嘗試並進一步解釋我的意思,讓我展示一下我的輸出示例。

4.75 hours were spent on the 41 week of 2013 
7.5 hours were spent on the 41 week of 2013 
4 hours were spent on the 43 week of 2013 
7.5 hours were spent on the 43 week of 2013 
7.5 hours were spent on the 43 week of 2013 
4.5 hours were spent on the 43 week of 2013 
6 hours were spent on the 43 week of 2013 
4 hours were spent on the 43 week of 2013 
7.5 hours were spent on the 43 week of 2013 
7 hours were spent on the 45 week of 2013 
8 hours were spent on the 45 week of 2013 
8 hours were spent on the 45 week of 2013 
4.5 hours were spent on the 45 week of 2013 
6 hours were spent on the 45 week of 2013 
7.5 hours were spent on the 45 week of 2013 
5.5 hours were spent on the 45 week of 2013 
8 hours were spent on the 45 week of 2013 

由於我if聲明不起作用,它只是創造了在我的數據庫中的每一行一個新的數組。但實際上我希望它做的是每週加起來的小時數是相同的。因此,第41周的所有時間將會加在一起,例如,小時數將是12.25。任何人都知道我能做什麼?

回答

1

代替使用自動遞增數組鍵,使用週數作爲密鑰:

else{ 
    $year = explode("-", $row['date']); 
    $time[$week] = array('week' => $week,'hours' => $row['hours'],'year' => $year[0]); 
} 

題外話:可能會更精確寫$year = explode("-", $row['date'], 2)[0](PHP> = 5.4),並使用$year直接,或者可能即使是$year = (int)$row['date'],因爲無論如何這種格式似乎是假定的。

這可以讓你「知道」用什麼鍵索引到多維數組,以鞏固工作時間:

if (isset($time[$week])){ 
    $time[$week]['hours'] += $row['hours']; 
} 

這樣就解決了PHP中的問題。但是,如果可能的話,最好在SQL中表示操作,並讓數據庫執行小時的分組和總和。

+0

我該如何使用週數作爲關鍵?我對此很新。 – user2989367

+0

@ user2989367:第一個代碼片段,'$ time [$ week] = ...'。但是你應該真的考慮直接把它寫成SQL。 – Jon

+0

哦抱歉沒有看到變化。 – user2989367

2

從目前爲止所說的話(糾正我,如果我錯了),你會需要group所有小時從week,從具體的users

如果這是正確的,在WEEK字段中添加一個GROUP BY子句應該產生應該給你想要的結果。

我只是寫了一個sqlFiddle,所以你可以看到我的意思:http://sqlfiddle.com/#!2/f49a3/2

如果是所需的最終結果,通過增加一個user到SQL,你將能夠只組和order by一週,讓你減少幾個小時。

如果我錯了,讓我知道有一個解釋,我會再試一次:)


編輯:

如果以上是正確的,那麼通過改變SQL,你應該能夠將代碼縮減爲類似於;

<?php 
include ('require.php'); 
include ('dblink.php'); 

$sql  = "SELECT `week`, `year` SUM(`hours`) AS `weekly_hours` FROM `events` WHERE `user` = '".$_SESSION['s_username']."' GROUP BY `week` ORDER BY `week` DESC"; 
$results = mysqli_query($con, $sql) or die(mysqli_error()); 
if (mysqli_num_rows($results) > 0) 
{ 
    while($row = mysqli_fetch_assoc($results)) 
    { 
     echo number_format($row['weekly_hours'], 2) . ' hours were spent on the ' . $row['week'] . ' week of ' . $row['year'] . '<br />'; 
    } 
} 
?> 
相關問題