dongmubi4444 2014-11-25 03:15
浏览 12
已采纳

获取按日期范围列的总和

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

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

//search1

<?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

<?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? 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

  • 写回答

3条回答 默认 最新

  • dongzi9196 2014-11-25 03:34
    关注

    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

    <?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

    <?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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应