简体   繁体   English

使用MySQL数据库的PHP中的日期范围报告

[英]Date range report in PHP using MySQL Database

I'm trying to achieve something similar to this question - link . 我正在尝试实现与此问题链接相似的功能。 I need to generate a PHP report using multiple variables from my database between a date range. 我需要使用日期范围内数据库中的多个变量来生成PHP报告。

Here's what I have so far : 这是我到目前为止的内容:

Here is my javascript: 这是我的JavaScript:

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

My data range form 我的数据范围表格

<div align="center">
<form name="date_range" id="date_range" method="post" style="width: 454px" >
<fieldset>
<legend>Start Date : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="start" id="datepicker"/>
<legend>End Date : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="end" id="datepicker2"/>
<button class="btn btn-inverse" type="submit" name="click" >
<i class="icon icon-print icon-white"></i> 
Run Report</button>
</fieldset>
</form></div>
<br><br>

My Results table 我的结果表

<div align="center">
  <table id="date_range" width="100%" cellspacing="0" cellpadding="10">
    <tr>
      <td align="center"><table width="300" cellpadding="4" cellspacing="0" class="product_box">
        <tr>
          <td align="center" bgcolor="#EAF0FF" class="column-light">&nbsp;</td>
          <td align="center" bgcolor="#EAF0FF" class="column-light"><strong> Sales: </strong></td>
          <td align="center" bgcolor="#EAF0FF" class="column-light"><strong>Sale Price Total:</strong></td>
          <td align="center" bgcolor="#EAF0FF" class="column-light"><strong>Cost Price Total:</strong></td>
          <td align="center" bgcolor="#EAF0FF" class="column-light"><strong>Margin:</strong></td>
              </tr>
        <tr>
      <td align="center" class="column-light"><img src="images/spacer.png" width="140" height="1" /></td>
          <td align="center" class="column-light"><img src="images/spacer.png" width="110" height="1" /></td>
          <td align="center" class="column-light"><img src="images/spacer.png" width="110" height="1" /></td>
          <td align="center" class="column-light"><img src="images/spacer.png" width="110" height="1" /></td>
          <td align="center" class="column-light"><img src="images/spacer.png" width="110" height="1" /></td>
           </tr>
        <tr>
          <td align="center" bgcolor="#F5F5F5" class="column-light"><strong>Bawtry</strong></td>
          <td align="center" bgcolor="#F5F5F5" class="column-light"><strong>

One of many SELECT statements: 许多SELECT语句之一:

$sql="SELECT * FROM shop_orders WHERE 
shop_order_day AND shop_order_month AND shop_order_year BETWEEN 'start' and 'end' AND shop_order_action = 'Sale' AND shop_order_location = 'Bawtry' AND shop_order_field_to_update LIKE '%con%' ORDER BY shop_order_id DESC ";

$result=mysql_query($sql);
$bawtry_sales = mysql_num_rows($result);
echo $bawtry_sales ?>
          </strong></td>
          <td align="center" bgcolor="#F5F5F5" class="column-light"><?php

... ...

I'm having problems making this work so that I can select the results of my query between shop_order_day, shop_order_month and shop_order_year using the dates chosen within the above form, ie 2014-03-09 - 2014-03-16. 我在执行此工作时遇到问题,因此我可以使用以上表格(即2014-03-09-2014-03-16)中选择的日期在shop_order_day,shop_order_month和shop_order_year之间选择查询结果。

So my question is what do I need to change to make either the SELECT statement correct or the Javascript functional? 所以我的问题是,我需要更改什么以使SELECT语句正确或Javascript功能正常?

Any help would be great - sorry if this is a little convoluted. 任何帮助都将是非常有用的-对不起,如果有点麻烦。

You'll be wanting this query. 您将需要此查询。

 SELECT * 
  FROM shop_orders 
 WHERE DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
       BETWEEN 'start' and 'end' 
   AND /* the other stuff in your query */

But you should know that this is grossly inefficient at searching. 但是您应该知道,这在搜索方面效率很低。 You'd be much better off with a shop_order_date column of type DATE with an index on it and the following query pattern: 您最好使用DATE类型的shop_order_date列,上面带有一个索引以及以下查询模式:

 SELECT * 
  FROM shop_orders 
 WHERE shop_order_date BETWEEN 'start' and 'end' 
   AND /* the other stuff in your query */

By the looks of your SQL statement I would say the problem is combination of the SQL keywords you want to use and your table structure. 从您的SQL语句的外观来看,我想说的问题是您要使用的SQL关键字和表结构的组合。 Instead of using separate columns for day, month and year you should consider use a single datetime column type. 不要考虑使用单独的日期,月份和年份列,而应考虑使用单个datetime列类型。 That way you will be able to use standard SQL functions that operate on date and time values. 这样,您将能够使用对日期和时间值进行操作的标准SQL函数。

Also. 也。 BETWEEN is best used with data types that are numbers. 最好将BETWEEN用于数字数据类型。 So, you could convert the date to a timestamp ... but why complicate things? 因此,您可以将日期转换为时间戳记...但是为什么使事情复杂化?

Set $start and $end to ISO standard dates - YYYY-MM-DD. 将$ start和$ end设置为ISO标准日期-YYYY-MM-DD。 Then: 然后:

SELECT * FROM shop_orders WHERE shop_order_date >= '$start' AND shop_order_date <= '$end' AND ... SELECT * FROM shop_orders WHERE shop_order_date> ='$ start'AND shop_order_date <='$ end'AND ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM