数据由MySQL安排

时间:2014-06-02 10:33:47

标签: mysql

我有一个MySQL数据库,其中包含存储物品销售等数据。

我希望根据每件商品的每日销售情况制作一个html显示表。

How do i produce a table of data to include the dates of where no items exist in the database on some of the days?

示例:

01-05-14 - 1 Sold
02-05-14 - 4 Sold
03-05-14 - 0 Sold
04-05-14 - 2 Sold
05-05-14 - 0 Sold

我可以根据表中存储的数据生成一个表,但是如何根据表中不存在的日期来管理数据?

假设我应该首先生成一个包含日期的表,然后根据每个日期执行MySQL查询,我是否正确?

1 个答案:

答案 0 :(得分:1)

我认为这可以解决你的问题,
使用dateRange函数获取日期列表并使用

<?php

function dateRange( $first, $last, $step = '+1 day', $format = 'd-m-y' ) {

    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);

    while( $current <= $last ) {

        $dates[] = date( $format, $current );
        $current = strtotime( $step, $current );
    }

    return $dates;
}

//print_r( dateRange( '2010-07-26', '2010-08-05') );
?>

这是打印html表的代码

 <table cellpadding="0" cellspacing="0" width="100%" class="sortable">

                            <thead>
                                <tr>
                                    <th>day</th>
                                    <th>no. items sold</th>

                                </tr>
                            </thead>

                            <tbody>
    <?php

            $dates=dateRange('2014-07-26','2014-07-30');\\dateRange('start date','end date')
          $result = mysqli_query("SELECT day,items FROM items_table");\\i just used a dummy table use your table here

 $output='';
          while ($row = mysqli_fetch_array($result)){
          if($dates[i]==$row['day'])
          {
            $output .= '
          <tr>
          <td>' . $row['day'] . '</td>
          <td>' . $row['items'] . '</td>                                                   
          </tr>';
          }
          else
          {
            $output .= '
          <tr>
          <td>' . $dates[i] . '</td>
          <td>0</td>                                                   
          </tr>';
          }
    $i++;
          echo  $output;
      }

    ?>
    </tbody>
    </table>