2014-11-25 128 views
0

我有一個很小的代碼行,將獲得一個列的總和獲取一列的總和

<?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範圍節目導致在同一個頁面

回答

1

首先,你需要添加其他功能:

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

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> 

你可以嘗試這樣的事情。

+0

我試過了你的代碼,並且我收到了通知消息「注意:未定義的變量: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

+0

嘗試在使用它之前聲明該變量。 – JuanSedano 2014-11-25 17:02:20

+0

我試圖在搜索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

0

首先,未指定日期和結束日期時,顯示所有總和。但是,如果這些存在,則可以過濾查詢以在日期範圍內顯示總和。

這是我的解決方案。

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; 
} 
0

現在是working.I重寫我的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!"; 
} 

?> 

,我也改變了Jquery的日期選擇器的日期格式,類似於MySQL的使用的格式,我發現有不同的日期格式在HTML($ _ GET方法)和MySQL使用的格式將導致查詢中爲空。