我是编程的新手,我遇到了一个错误,经过几个小时的搜索我无法解决。
我正在使用表单来执行搜索并在表格中显示数据,这部分相当容易......但我也想计算最终结果中有多少天,而不是我第一次使用的日期询问。
该查询在 2 个日期和指定公司之间的天数内搜索结果,问题出在最终结果中,它计算的是我的结果有多少天,而不是我搜索的天数。
示例:如果我在 2 月 11 日和 2 月 20 日之间搜索 2 个日期,其中公司 = 1
如果公司 1 只有 3 天的结果,我希望它只计算 3 天。
我知道我的代码在那部分是错误的,但我无法让它做我想做的事。
这是我的一小段代码来解释它:
if (isset($_POST['search'])) {
$date1 = mysql_prep($_POST['date1']);
$date2 = mysql_prep($_POST['date2']);
$latte = mysql_prep($_POST['latte']);
$query = "SELECT * FROM payroll WHERE (day BETWEEN '{$date1}' AND '{$date2}') AND (company = '{$latte}') ORDER BY day ASC ";
$result = mysql_query($query, $connection);
$woof = "SELECT SUM(hours) FROM (SELECT * FROM payroll WHERE (day BETWEEN '{$date1}' AND '{$date2}') AND (company = '{$latte}') ORDER BY day ASC) AS subt ";
$raw = mysql_query($woof, $connection);
if(!$raw) { die(mysql_error());}
$meow = mysql_result($raw, 0, 0);
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24) +1);
if(!$result) {
echo "FAIL";
} else {
echo "<table>
<tr>
<th> Date </th>
<th> Hours </th>
<th> Job Title </th>
<th> Job Description </th>
<th> Paid </th>
<th> For </th>
</tr>";
while($row = mysql_fetch_array($result))
{
$company = $row['company'];
if($company == 0) {
$company = "Wilson Electric";
} if($company == 1) {
$company = "Wilson Rental";
} if ($company == 2) {
$company = "Church of Christ";
}
echo "<tr>";
echo "<td class=\"center\">" . $row['day'] . "</td>";
echo "<td class=\"center\">" . $row['hours'] . "</td>";
echo "<td style=\"padding:5px;\">" . $row['job_title'] . "</td>";
echo "<td style=\"padding:5px;\">" . $row['job_description'] . "</td>";
echo "<td class=\"center\">" . $row['paid'] . "</td>";
echo "<td style=\"padding:5px;\">" . $company . "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td class=\"center\"> Total Days: " . $days . "</td>";
echo "<td class=\"center\"> Total Hours: " . $meow . "</td>";
echo "</tr>";
}
}
感谢您阅读它,我希望有人能给我一个解决方案,我知道一定很简单,但我就是想不通。