请求后汇总列

时间:2015-09-04 19:29:22

标签: php mysqli

我认为这需要在DB请求之后完成。我似乎无法绕过这一个。下面的代码完全可以作为搜索功能...我需要帮助这个其他部分..

我正在尝试创建一个允许用户搜索时间表活动的搜索。到目前为止,我已将其设置为按名称,日期和&搜索活动类型。如果选择第4个选项,我希望根据名称和活动计算总小时数。

+--------+----------+-----------+----------+-------------+
|  Name  | TimeIn   | TimeOut   |   Date   |  Activity   |
+--------+----------+-----------+----------+-------------+
| Bill   |1441309378|1441309382 | 2015-9-1 | Office      |
| Bob    |1441309378|1441309382 | 2015-9-1 | Field       |
| Jill   |1441309378|1441309382 | 2015-9-1 | Sales       |
| Bill   |1441309378|1441309382 | 2015-9-1 | sales       |
| Bill   |1441309378|1441309382 | 2015-9-1 | sales       |
+--------+----------+-----------+----------+-------------+

在选择活动时使用上面的示例数据库,代码使用存储在数据库中的预先存在的活动类型来捆绑每个名称的每个类型并将小时数相加。所以你会看到

Name: Bill - Activity: Office - Duration: 4
Name: Bill - Activity: Sales - Duration: 8
Name: Jill - Activity: Sales - Duration: 4
Name: Bob - Activity: Field - Duration: 4

选择名称时

Name: Bill - Activity: Office - Duration: 12
Name: Jill - Activity: Sales - Duration: 4
Name: Bob - Activity: Field - Duration: 4

到目前为止我的代码:

<!DOCTYPE html>
<html>
<body>
<head>
<style>
fieldset { border:0px}
</style>
</head>
<form action="payc.php" method="post">
Pay Range 
  <input type="date" name="start">
<input type="date" name="stop">
          <span>Name:</span>
<?php
$conn = mysqli_connect("localhost", "user", "Pass123", "DB");

$sql = "SELECT NAME FROM EMP";
$query = mysqli_query($conn, $sql);

echo '<select name="EMP">';
echo '<option value="n">Select</option>';
echo '<option value="A">All</option>';
while($empname = mysqli_fetch_assoc($query)){
echo '<option value="' . $empname['NAME'] . '">' . $empname['NAME'] . '</option>';
}
echo '</select>';
?>
        <span>Activity:</span>
        <?php
$conn = mysqli_connect("localhost", "user", "Pass123", "DB");

$sql = "SELECT NAME, DIS_NAME FROM INVITEM";
$query = mysqli_query($conn, $sql);

echo '<select name="item">';
echo '<option value="n">Select</option>';
echo '<option value="A">All</option>';
while($empname = mysqli_fetch_assoc($query)){
echo '<option value="' . $empname['NAME'] . '">' . $empname['DIS_NAME'] . '</option>';
}
echo '</select>';
?>

<input type="submit">
</form>
<form action="payc.php" method="post">
<span> Calculate time by:</span>
<select name="comp">
<option value="2">Activity</option>
<option value="1">Name</option>
<option value="0">Time Log</option>
</select>
<input type="submit">
</form>
</body>
</html>

<?php
$servername = "localhost";
$username = "user";
$password = "Pass123";
$dbname = "DB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 





if($_POST['EMP']=="n"){

if($_POST['item']=="n"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."'";
$result = $conn->query($sql);
}
}
if($_POST['item']!=="n"){
if($_POST['item']!=="A"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."' AND ITEM= '". $_POST['item']."'";
$result = $conn->query($sql);
}
if(empty($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE ITEM= '". $_POST['item']."'";
$result = $conn->query($sql);
}
}
}
if($_POST['item']=="A"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."'";
$result = $conn->query($sql);
}
if(empty($_POST['start'])){
$sql = "SELECT * FROM TIME";
$result = $conn->query($sql);
}
}

}


if($_POST['EMP']!=="n"){
if($_POST['EMP']!=="A"){

if($_POST['item']=="n"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."' AND EMP= '".$_POST['EMP']."'";
$result = $conn->query($sql);
}
if(empty($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE EMP= '".$_POST['EMP']."'";
$result = $conn->query($sql);
}
}
if($_POST['item']!=="n"){
if($_POST['item']!=="A"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."' AND ITEM= '". $_POST['item']."' AND EMP= '".$_POST['EMP']."'";
$result = $conn->query($sql);
}
if(empty($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE ITEM= '". $_POST['item']."' AND EMP= '".$_POST['EMP']."'";
$result = $conn->query($sql);
}
}
}
if($_POST['item']=="A"){
if(isset($_POST['start'])){
if($_POST['EMP']!=="A"){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."' AND EMP= '".$_POST['EMP']."'";
$result = $conn->query($sql);
}
if($_POST['EMP']=="A"){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."'";
$result = $conn->query($sql);
}
if($_POST['EMP']=="n"){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."'";
$result = $conn->query($sql);
}
}
if(empty($_POST['start'])){
if($_POST['EMP']!=="A"){
$sql = "SELECT * FROM TIME WHERE EMP= '".$_POST['EMP']."'";
$result = $conn->query($sql);
}
if($_POST['EMP']=="A"){
$sql = "SELECT * FROM TIME";
$result = $conn->query($sql);
}
if($_POST['EMP']=="n"){
$sql = "SELECT * FROM TIME";
$result = $conn->query($sql);
}
}
}
}

}


if($_POST['EMP']=="A"){

if($_POST['item']=="n"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."'";
$result = $conn->query($sql);
}
if(empty($_POST['start'])){
$sql = "SELECT * FROM TIME";
$result = $conn->query($sql);
}
}
if($_POST['item']!=="n"){
if($_POST['item']!=="A"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."' AND ITEM= '". $_POST['item']."'";
$result = $conn->query($sql);
}
if(empty($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE ITEM= '". $_POST['item']."'";
$result = $conn->query($sql);
}
}
}
if($_POST['item']=="A"){
if(isset($_POST['start'])){
$sql = "SELECT * FROM TIME WHERE STAMP BETWEEN '". $_POST['start']."' AND '".$_POST['stop']."'";
$result = $conn->query($sql);
}
if(empty($_POST['start'])){
$sql = "SELECT * FROM TIME";
$result = $conn->query($sql);
}
}

}






if ($result->num_rows > 0) {
  echo "<span>Log:</span><span>Date:</span><span>Name:</span><span>Job:</span><span>Duration:</span><span>Notes:</span>";

echo "<form action=\"test.php\" method=\"post\">";

    while($row = $result->fetch_assoc()){
$duration=(($row['T_OUT']-$row['T_IN'])/60)/60;
if($duration < 0){
$duration = "Did not clock out.";
}
else{
$duration = $duration;
}
$stamp=$row['STAMP'];
$datetime = explode(" ",$stamp);
$date = $datetime[0];

        echo "<fieldset>Log: " .$row['LOG']. " Date: " .$stamp. " <input type=\"hidden\" name=\"timeact[]\" value=\"TIMEACT\"><input type=\"hidden\" name=\"date[]\" value=\"".$datetime[0]."\">Name:<input name=\"EMP[]\" type=\"text\" value=\"". $row['EMP']. "\">Job:<input name=\"ITEM[]\" type=\"text\" value=\"" . $row['ITEM']. "\">Duration:<input name=\"time[]\" type=\"text\" value=\"" . $duration. "\">Notes:<input name=\"NOTE[]\" type=\"text\" value=\"" . $row['NOTE']. "\"></fieldset>";

        }


echo "<input type=\"submit\">";
echo "</form>";
} else {
    echo "0 results";
}

$conn->close();
?>

0 个答案:

没有答案