如何在mysql中生成具有特定日期范围的报告

时间:2013-07-12 02:43:12

标签: php mysql pdo report

我已经在使用商店收入的每日报告,但我需要我的报告执行以下操作:我希望查看特定日期,周或月的收入报告....我可以选择我想看的日期或天数,以便我可以打印我的选择。

这里是inc.php

<?
$dbtype     = "mysql";
$dbhost     = "localhost";
$dbname     = "anillos";
$dbuser     = "rubi";
$dbpass     = "----";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->exec("set names utf8");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
?>

这是我的代码:

<table class="table-bordered table-striped table-condensed">
    <thead>
        <tr>
          <th>Fecha</th>
          <th>Total Anillos Vendidos</th>
          <th>Total ganado del día</th>                                 
        </tr>
    </thead>   
    <tbody id="result_fechas">
        <tr>
            <? 
                include 'inc.php';
                $sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_diario_ganado
                FROM PRODUCTOS WHERE start >= CURDATE() AND start < CURDATE() + INTERVAL 1 DAY ORDER BY start ASC");
                $sql->execute();
                while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
            ?>
            <td class="center"><? echo $row['date']; ?></td>
            <td class="center"><? echo $row['total_anillos']; ?></td>
            <td class="center"><? echo $row['total_diario_ganado']; ?></td>                 
        </tr>   <? } ?>                          
    </tbody>

你能帮我解决这个问题......我真的没有线索......

---- ----- UPDATE

@ Chris78我做了这个表格:

<form name="fechas" id="fechas" method="post" >
<fieldset>
<legend>Reporte desde : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="f_desde" id="datepicker"/>
<legend>Reporte hasta : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="f_hasta" id="datepicker2" />
<button class="btn btn-inverse" type="submit" name="enviar" >
<i class="icon icon-print icon-white"></i> 
Ver Reporte                                     </button>
</fieldset>
</form>

新的SELECT CODE:

<? 
$sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_ganado FROM PRODUCTOS WHERE start BETWEEN 'f_desde' AND 'f_hasta' ORDER BY start ASC");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
?>

表:

<table class="table-bordered table-striped table-condensed">
    <thead>
        <tr>
          <th>rango de fechas seleccionado</th>
          <th>Total Anillos Vendidos</th>
          <th>Total ganado</th>                                 
        </tr>
    </thead>   
    <tbody id="result_fechas">
        <tr>
            <? 
           ////////SELECT CODE HERE
            ?>
            <td class="center"><? echo $row['date']; ?></td>
            <td class="center"><? echo $row['total_anillos']; ?></td>
            <td class="center"><? echo $row['total_ganado']; ?></td>                 
        </tr>   <? } ?>                          
    </tbody>

我尝试在同一页面中使用ajax调用显示结果

<script type="text/javascript">
      $(function(){
        $("#fechas").submit(function(){
          $.ajax({
            type:"POST",
            url:".reportes.php?ts=" + new Date().getTime(),
            dataType:"text",
            data:$(this).serialize(),
            beforeSend:function(){
              $("#loading").show();
            },
            success:function(response){
                $("#result_fechas").append(response);
                $("#loading").hide();
            } 
          })
          return false;
        });
    });
    });
    </script>

reportes.php是同一个页面,其中是表单和等待结果的表....但我不知道错误在哪里,因为没有捕获ajax数据,当我的页面刷新按钮...你可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您创建了一个表格,其中包含您所谈到的那两个输入,当您检索输入值时,您给予处理“date('Y-m-d',inputvalue)”(如果尚未处理)。然后在sql语句中替换已处理的输入值的“CURDATE()”。

日期应始终以“yyyy-mm-dd”格式提供给MySql。

来源:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html

答案 1 :(得分:0)

如果您想要使用日期范围运行query,可以使用BETWEEN声明。

$sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_diario_ganado
                FROM PRODUCTOS WHERE start BETWEEN 'Older Date Here' AND 'Newer Date Here' ORDER BY start ASC");