以相同的html格式显示php日期范围搜索的结果

时间:2014-11-26 14:27:12

标签: php html mysql

我想在数据库基础上搜索日期范围。我可以在mysql控制台中获得结果:

SELECT SUM(amount) as sum from serviceces where date_of_service between '2014-11-06' and '2014-111-    12';

//以我的形式

<?php
    include_once('../../libs/search/search_sales_by_date.php');
    include_once('../../libs/search/total_sales.php');
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!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({ dateFormat: 'mm-dd-yy' });
            });
        </script>
    </head>
    <body>
        <div>
        <?php
            if(isset($error)) {
                echo $error;
            }
            if(isset($success)) {
                echo $success;
            }
        ?>
        </div>
        <h3>Search Sales</h3>
        <p>From</p>
        <form method="GET" 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>

// search.php中

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

        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 {
            $total_by_date_range = 0;
            $total_by_date_range = $init->getSumByDateRange($date_from, $date_to);
            if($total_by_date_range== 1) {
                $success ="Total calculated base on your search: ".$total_by_date_range;
            } elseif (!$total_by_date_range) {
                $error = "error";
            } else {
                $error = "Record not found due to some errors";
            }
        }
    }
?>

// class.ManageServices.php

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

我的问题是,如果我在表单中执行echo $ total_date_by_range,它什么都不显示,执行vardump($ total_by_date_range)显示为null。我的代码出了什么问题?我还需要在同一表格上显示结果。关于如何实现这一点的任何想法?我把它放在表格的顶部,因为它会显示通知未识别的索引.....每次我提交按钮。

更新..

vardump($ total_by_date_range)导致

array (size=1)
0 => 
array (size=2)
  'sum_of_date_range' => null
  0 => null

为什么'sum_of_date_range'为空?

0 个答案:

没有答案