2014-01-19 51 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 />'; 
    } 
} 
?> 
相关问题