使用月份和年份过滤表(PHP MYSQL)

时间:2017-06-02 03:52:01

标签: php

我应该制作一个搜索功能,允许用户使用月份和年份过滤他们的表格。 enter image description here

这是界面.. enter image description here

用户应该可以搜索“发布日期”'通过选择月份和年份。但我不知道该怎么做。

    <div class="col-sm-3">
   <select class="form-control" name="month">
       <option>Select Month</option>
       <option value="1">January</option>
       <option value="2">February</option>
   </select>
</div>
<div class="col-sm-3">
<select class="form-control" name="yearManufactured" required>  
<option>Select Year</option>
  <?php
    foreach(range(1950, (int)date("Y")) as $year) {
      echo "\t<option value='".$year."'>".$year."</option>\n\r";
  }

  ?>
</select>  
</div>
有人可以指导我吗?或者给出了相关示例或教程的任何链接..

2 个答案:

答案 0 :(得分:0)

您可以像这样查询,$ month和$ year是$ _REQUEST值:

$month = strlen(trim($month)) == 1 ? "0".$month : $month;

$searchDate = $year."-".$month;

SELECT * FROM $table WHERE dateissued like '$searchDate%'

希望这会有所帮助:)

答案 1 :(得分:0)

您可以在php中添加01,02,03...之类的月份,并在mysql中使用extract

<select class="form-control" name="month">
       <option>Select Month</option>
       <option value="01">January</option>
       <option value="02">February</option>
       .......
</select>

<select class="form-control" name="yearManufactured" required>  
<option>Select Year</option>
  <?php
    foreach(range(1950, (int)date("Y")) as $year) {
      echo "<option value='".$year."'>".$year."</option>\n\r";
  }

  ?>
</select>

PHP:

$where =' where 1=1';
if(isset($_POST['month']) && !empty($_POST['month']))
{

    $where.=" and EXTRACT(MONTH from dateIssued)='".$_POST['month']."'";

}
if(isset($_POST['yearManufactured'])  && !empty($_POST['yearManufactured']))
{

    $where.=" and EXTRACT(YEAR from dateIssued)='".$_POST['yearManufactured']."'";

}

$query ="select * from table_name $where";