[英]How can I generate a statistical date range report in PHP and MySQL?
I am trying to make a date range report and this is what I have for far. 我正在尝试制作日期范围报告,这是我到目前为止所掌握的。
Form 形成
<form class="daterangeform" method="POST" action="datereport.php">
<fieldset id="daterangefield">
<legend><b>Date Range Report</b></legend>
<div class= "group1">
<div class = "group2">
<label for="fromdate"> From </label>
<br/>
<input type="date" name="fromdate" required/>
</div>
<div class = "group2">
<label for="todate"> To </label>
<br/>
<input type="date" name="todate" required/>
</div>
<br/>
</div>
</fieldset>
<br/>
<div class="formbuttons">
<input name= "formsubmit" id="formsubmit" type="submit" value="Generate Report" />
</div>
</form>
MySQL query (it shows 'success') MySQL查询(显示“成功”)
if(isset($_POST['formsubmit']))
{
$fromdate=mysql_real_escape_string($_POST['fromdate']);
$todate=mysql_real_escape_string($_POST['todate']);
$sql = "select crime_type, count(*) from cases where commitment_date between '$fromdate' and '$todate' group by crime_type;";
if($db->query($sql))
{
echo "success";
}
else
{
echo(mysql_error());
}
}
This is what I want to achieve. 这是我想要实现的。 For instance, there are crime types 'Robbery', 'Assault' and 'Traffic Accident'. 例如,有犯罪类型“抢劫”,“攻击”和“交通事故”。 When I select a date range and submit the form, I want it to count the number of robberies, assaults and traffic accidents in that particular range and display it in a table. 当我选择日期范围并提交表格时,我希望它计算该特定范围内的抢劫,殴打和交通事故的数量并将其显示在表格中。 I am not sure about the above query working for this. 我不确定上面的查询是否为此工作。 Also, kindly assist me in how to display the values since I am a beginner. 另外,由于我是初学者,请帮助我如何显示值。 Thank you in advance. 先感谢您。
Here is a small bit that may help. 这可能会有所帮助。 You should also switch to using mysqli functions but that is another topic. 您还应该切换到使用mysqli函数,但这是另一个主题。 I didn't test this so it could throw errors but this is the general idea. 我没有对此进行测试,因此可能会引发错误,但这是一般的想法。
if(isset($_POST['formsubmit']))
{
$fromdate=mysql_real_escape_string($_POST['fromdate']);
$todate=mysql_real_escape_string($_POST['todate']);
##Added the count alias##
$sql = "SELECT crime_type, count(*) as count
FROM cases
WHERE commitment_date
BETWEEN '$fromdate' and '$todate'
GROUP BY crime_type;";
##assigned the query to a variable##
$result = $db->query($sql);
##if at least 1 result##
if($result && (mysql_num_rows($result) > 0))
{
##echo everything into a table##
echo "<table><thead><tr><th>Crime type</th><th>Count</th></tr></thead>";
while ($row = mysql_fetch_assoc($result))
{
echo "<tr><td>".$row['crime_type']."</td><td>".$row['count']."</td></tr>";
}
echo "</table>";
}
else
{
##There won't be an error thrown for an empty result set##
echo "<h2>No records found...</h2>";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.