How Make reports with specific range of dates in mysql

别说谁变了你拦得住时间么 提交于 2019-12-12 03:16:43

问题


I am already using a daily report of the income of the store, but I need my report to do the following: i want to see the income report of specific range of dates, or weeks, or months.... essentially that i can choose which days or range of days i want see so i can print my selection.

here is 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 );
?>

here is my code:

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

can you help me with this..I really not have a clue...

----UPDATE-----

@ Chris78 I made this form:

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

the new 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)) {
?>

the table :

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

and I am try to show the result with ajax call in the same page

<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 is the same page where is the form and the table which is waiting the result....but I don´t know where is the error, because not catch ajax the data and the page is refreshing when I clic the button...can you help me with this.


回答1:


If you are wanting to run a query with a date range you can use the BETWEEN statement.

$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");



回答2:


You create a form with those two inputs that you talked about, when you retrieve the input value you give the treatment "date('Y-m-d', inputvalue)" if it isn't already treated. Then you replace "CURDATE()" on you sql statement for the treated inputvalues.

Dates should always be given in the "yyyy-mm-dd" format to MySql.

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



来源:https://stackoverflow.com/questions/17606704/how-make-reports-with-specific-range-of-dates-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!