简体   繁体   English

按日期范围获取列的总和

[英]Get sum of a column by date range

I have a small line of code that will get the sum of a column 我有一小段代码将获得一列的总和

<?php

 class ManageServices{

 function getTotalSumByDateRange()
 {
    $query = $this->link->query("SELECT SUM(amount) as sum_of_date_range FROM services ");      

    $rowcount = $query->rowCount();

    $result = $query->fetchAll();
    return $result; 
}
}

?>

//search.php //search.php

<?php
 include_once('../../classes/class.ManageServices.php');
 $init = new ManageServices();  
 $sum_of_date_range = $init->getTotalSumByDateRange();    
 ?>

//search1 //搜索1

<?php

if(isset($_POST['submit']))
{ 
include_once('../../classes/class.ManageServices.php');
$init = new ManageServices();  
$date_from =$_POST['to_date'];
$date_to =$_POST['from_date'];

if(empty($_POST['to_date']) && empty($_POST['from_date']))
{
    $error = "No search query";
}
elseif(empty($_POST['to_date']) && !empty($_POST['from_date']))
{
     $error ="Please specify your start date search";
}
elseif(!empty($_POST['to_date']) && empty($_POST['from_date']))
{
     $error ="Please specify your end date search";
}
else
{
    $total_by_date_range = 0;
    $total_by_date_range = $init->getSumByDateRange($date_from, $date_to);
}
}

 ?>

//html // HTML

<?php
include_once('../../libs/search/search_sales_by_date.php');
include_once('../../libs/search1/total_sales.php');
?>

<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>search by dates</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( ".datepicker" ).datepicker();
});
</script>

</head>
<body>
<div>
<?php
if(isset($error))
{
  echo $error;
}
?>
</div>
<h3>Search Sales</h3>
<p>From</p>
<form method="POST" action="search_sales_by_dates.php">
<p>Date: <input type="text" class="datepicker" name="from_date" id="field1"></p>
<p>To</p>
<p>Date: <input type="text" class="datepicker" name="to_date" id="field2"></p><br />
<input type="submit" value="search" name="submit" id="submitdata">
</form>
<div id ="fillmein1">
<?php foreach($sum_of_date_range as $sum): ?>
  <td>Total Sales<span class="glyphicon glyphicon-usd" aria-hidden="true"></span><?php echo     number_format($sum['total_sum'],2); ?></td>  
 <?php endforeach; ?>
 </div>
 <input type="hidden" value ="$total_by_date_range['sum_of_date_range'] ">//this is the problem
 </body>
 </html>

and in my table i have columns 'id','amount',date_of_service'.The query above will calculate all the 'amount' values and display in html, however, i want to add another query that will only get the sum of 'amount' column base on a date range input from html form.Any ideas on this? 在我的表中我有列'id','amount',date_of_service'。上面的查询将计算所有的'amount'值并以html显示,但是,我想添加另一个查询,该查询只会得到'金额”列基于从html表单输入的日期范围。对此有何想法? Update..I think I'm almost there,after I updated search1.php,search.php,and html my problem now is I want to show the $total_by_date_range in same form.But when I submit the form, it shows no error but will not show the $total_by_date_range.anyway,the $sum_of_date range shows result in the same page 更新..我想我快到了,在更新search1.php,search.php和html之后,我现在的问题是我想以相同的形式显示$ total_by_date_range。但是当我提交该表单时,它没有显示错误但不会显示$ total_by_date_range。无论如何,$ sum_of_date范围在同一页面中显示结果

First you need to add another function: 首先,您需要添加另一个功能:

class ManageServices {
  function getTotalSumByDateRange() {
     $query = $this->link->query("SELECT SUM(amount) as sum_of_date_range FROM services ");      
     $rowcount = $query->rowCount();
     $result = $query->fetchAll();
     return $result; 
  }
  function getSumByDateRange($date_from, $date_to) {
     $query = $this->link->query("SELECT SUM(amount) as sum_of_date_range FROM services where
     date_of_service between '".$date_from."' and '".$date_to."'");      
     $rowcount = $query->rowCount();
     $result = $query->fetch();
     return $result; 
  }
}

Search.php search.php中

<?php
 include_once('../../classes/class.ManageServices.php');
 $init = new ManageServices();  
 $sum_of_date_range = $init->getTotalSumByDateRange();
 $total_by_date_range = $init->getSumByDateRange($_POST['from_date'],$_POST['to_date']); 
?>

HTML HTML

<?php
include_once('../../libs/search/search_sales_by_date.php');
?>

<div>
<?php foreach($sum_of_date_range as $sum):?> 
    <td><span class="glyphicon glyphicon-usd" aria-hidden="true"></span><?php echo    number_format($sum['sum_of_date_range'],2); ?></td>  
<?php endforeach;?>
<?php
  echo "Total by date range: ".$total_by_date_range;
?>
</div>

You can try something like this. 您可以尝试这样。

First, when from date and end date is not specified, you display all the sum. 首先,如果未指定起始日期和结束日期,则显示所有金额。 But if those exists you filter your query to display sum within the date range. 但是,如果存在这些内容,则可以过滤查询以显示日期范围内的总和。

Here is my solution. 这是我的解决方案。

function getTotalSumByDateRange($fromDate = "", $toDate = "")
{
    $sql = "SELECT SUM(amount) as sum_of_date_range FROM services ";      

    if (!empty($fromDate) && !empty($toDate) {
       $sql .= "WHERE date_of_service BETWEEN '". $fromDate . "' AND '" . $toDate . "'";
    }

    $query = $this->link->query($sql);

    $rowcount = $query->rowCount();

    $result = $query->fetchAll();
    return $result; 
}

Now it's working.I rewrite my search.php 现在工作了。我重写了search.php

<?php
if(isset($_REQUEST['submit']))
{
include_once('../../classes/class.ManageServices.php');
$init = new ManageServices();  
$date_to =$_REQUEST['to_date'];
$date_from =$_REQUEST['from_date'];

if(empty($date_from) && empty($date_to))
{
    $error = "No search query";

}
elseif(empty($date_from) && !empty($date_to))
{
    $error = "Specify your end date search";
}
elseif(!empty($date_from) && empty($date_to))
{
    $error ="Specify your start date search";
}
else
{
    if($date_from < $date_to)
    {
    $total_by_date_range = $init->getSumByDateRange($date_from, $date_to);
    foreach ($total_by_date_range as $key) 
    {
    $success ="Your Total amount of sales from ". $date_from . ' ' . "To" . ' ' .$date_to . ' '   ."is". ' ' ."P" .number_format($key['sum_of_date_range'],2);
    }
    }
    else
    {
        $error = "You bitch, Start date should not greater than the end date on your search  query";
    }
 }
}
else
{
$error = "Because you are an idiot, the script contains some bugs that is why it can't run on the   browser!";
}

?>

and I also changed the date format of Jquery datepicker, similar to the format used by mysql,I found out that having different date format in html($_GET method) and the format used by mysql will result null in query. 并且我还更改了Jquery datepicker的日期格式,类似于mysql所使用的格式,我发现html($ _ GET方法)中的日期格式与mysql所使用的格式不同会导致查询中的null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM