2013-07-12 45 views
0

我已经在使用店铺收入的每日报告,但我需要我的报告执行以下操作:我想查看特定日期范围的收入报告,或几周或几个月......基本上,我可以选择我希望看到哪几天或哪几天,以便我可以打印我的选择。如何在mysql中使用特定日期范围的报告

这里是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> 

你可以帮我this..I真的没有线索...

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

新选择代码:

<? 
$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的数据和页面刷新时,按钮...你能帮助我吗?

回答

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"); 
+0

您可以简单地将您的字段发布到'variable'并运行'query'。例如:'$ fromdate = $ _POST ['f_desde']; $ todate = $ _POST ['f_hasta'];''WHERE start BETWEEN'“。$ fromdate。”'AND'“。$ todate。”''希望能帮助你。 – Chris78

+0

谢谢你们这些变数让它成功!最好的祝福! –

+0

非常欢迎您!乐意效劳 :) – Chris78

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

+0

yes是的,但现在它只显示每天的销售额,我需要看到销售的天数范围......我想我需要制作一个窗体来插入特定的天数范围,如输入到一个按钮显示那些日子的销售情况..如2013年7月1日至2013年7月11日的销售额或2011年至2012年的销售额 –

+0

这应该是评论,而不是回答。 –

+0

更改我的答案,因为这不是一个答案,正如指出的那样。对不起,感谢您的建议 –

相关问题