我有一个很小的代码行,将获得一个列的总和获取一列的总和
<?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();
?>
//搜索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
<?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>
在我的表中,我有'id','amount',date_of_service'列。上面的查询将计算所有'amount'值并显示在html中,但是,我想添加另一个查询,只会得到'amount'栏的总和基于从html form输入的日期范围。有关于此的任何想法? 更新..我想我几乎在那里,我更新search1.php,search.php和html后,我现在的问题是我想显示$ total_by_date_range相同的形式。但是当我提交表单时,它显示没有错误,但不会显示在$ total_by_date_range.anyway中,$ sum_of_date范围节目导致在同一个页面
我试过了你的代码,并且我收到了通知消息“注意:未定义的变量:total_by_date_range在C:\ wamp \ www \ php_oop \ forms \ search \ search_sales_by_dates.php上59行”......我将search.php修改为?“ getSumByDateRange($ date_from,$ DATE_TO); } 别的 { $ error =“您的搜索结果h无效“; } } ?>仍然收到通知。 –
rai
2014-11-25 16:49:13
尝试在使用它之前声明该变量。 – JuanSedano 2014-11-25 17:02:20
我试图在搜索php中声明$ total_by_date_range = 0,警告通知消失了,但它给了我一个零结果,并且我尝试了var_dump($ total_by_date_range)它给出了'Int 0'结果,var_export($ total_by_date_range)它给了我'0'的结果; – rai 2014-11-26 01:18:14