PHP - 无法按月从MySQL数据库查询中过滤

时间:2013-11-10 11:30:13

标签: php html mysql

所以我正在用我当地的拼贴画做我的学生工作,我们必须用给定的主题创建整个网站。我选择停车场主题,我卡住了。坚持,不知道在哪里寻找问题。我需要做的是从数据库中过滤我给定的年份和月份数据,如下所示:

Database Table

和我的查询,从这个表中排序数据看起来像这样:

SELECT COUNT(parking_time) AS "Amont", SUM(sum) AS "Sum" 

    FROM parking_info

    GROUP BY MONTH(parking_time), YEAR(parking_time).

以及此查询的输出:

Output from following query

如您所见,用户应从DROPDOWN列表中选择年份和月份,以选择特定月份来过滤MySQL Query中的数据。例如:如果用户从10月下拉列表中选择 2013和,则应仅为用户输出特定月份信息,即金额 4 / 总和 170.但在我的情况下,它不会输出任何内容(正如您在我的网页中看到的那样)。

基本上我有 functions.php 文件,其中包含所有计数,过滤器和SQL查询。我对此特定页面的 functions.php 文件如下所示:

<?php 



function cleanFilterArray($filters) {

    $new_filters = array();

    if ($filters) {

        foreach ($filters as $field => $data) {

            if ($data != '%%') {

                $new_filters[$field] = $data;

            }

        }

        return $new_filters;

    } else {

        return false;

    }

}



function get_months_in_LT()

{

    $months = array();

    $months[] = array(1, 'january');

    $months[] = array(2, 'february');

    $months[] = array(3, 'march');

    $months[] = array(4, 'april');

    $months[] = array(5, 'may');

    $months[] = array(6, 'june');

    $months[] = array(7, 'july');

    $months[] = array(8, 'august');

    $months[] = array(9, 'september');

    $months[] = array(10, 'october');

    $months[] = array(11, 'november');

    $months[] = array(12, 'december');



    return $months;

}

function get_month_in_LT($num)

{

    $months = get_months_in_LT();

    return $months[$num][1];

}

function get_month_report($filters) {



    $where = null;



    if ($filters) {

            $filters = cleanFilterArray($filters);



            foreach ($filters as $field => $data) {

                if (!empty($data)) {

                    $where .= $field.' \''.$data.'\' AND ';

                }

            }

            if ($where) {

                $where = 'WHERE '.substr($where, 0, -5); //removes last 'AND' and adds a 'WHERE' to the begining

            }

    }

    var_dump($where);

    var_dump($filters);

    $result = mysql_query('

    SELECT COUNT(parking_time) AS "Amont", SUM(sum) AS "Sum" 

    FROM parking_info

    GROUP BY MONTH(parking_time), YEAR(parking_time)

    '.$where.'

    ');



    $rows = array();

    while ($row = mysql_fetch_row($result))

    {       

        $rows[] = $row; 

    }       



    return $rows;   

}

?>

我的实际页面包含所有HTML和所有其他内容,如下所示:

<?php 



include_once 'functions.php';



if (isset($_GET['submit']))

{

  $year = $_GET['year'];

  $month = $_GET['month'];

}

else

{

  $year = date('Y');

  $month = date('m');

}





if (isset($_GET['submit']))

{

  if (isset($year) && $year > (int) date('Y'))

  {

    $error = 'this years has not yet come!';

  }

  if (isset($month) && $month > (int) date('m'))

  {

    $error = 'this month has not yet come';

  }

  elseif (isset($year) && $year < 0)

  {

    $error = 'date cannot be negative';    

  }

  elseif (isset($month) && ($month < 1 || $month > 12))

  {

    $error = 'not a month';

  }    


}



if (isset($_GET['submit']))

{

  $year = $_GET['year'];

  $month = $_GET['month'];



  $filters = array(

    "parking_time LIKE" => '%'.$year.'%',

    "parking_time LIKE" => '%'.$month.'%'

  );



}


include_once 'mysql.php';

if (isset($_GET['submit']) && !isset($error)){

  $infos = get_month_report($filters);  

}

$months = get_months_in_LT();



var_dump($infos);

?>



<?php include_once 'header.php' ?>

    <h1>Month Report</h1>

    <div id="main">

      <p>How much per month was observed cars and what is the total amount of money collected</p>

      <?php 

        if (isset($error))

        {

          echo "<p class=\"error\">Klaida: $error</p>";

        }    

      ?>

      <form method="post">

        <table border="1"> 

          <tr>

            <th>Year<span style="color:red">*</span></th>

            <td><input type="text" name="year" value="<?php if (isset($year)) echo $year ?>" /></td>

          </tr>

          <tr>

            <th>Month</th>

            <td>

              <select name="month" style="width:100%">

                <?php 

                  foreach ($months as $montha)

                  {

                    if (isset($month) && $month == $montha[0])

                    {

                      echo "<option value=\"$montha[0]\" selected=\"selected\">$montha[1]</option>";

                    }

                    else

                    {

                      echo "<option value=\"$montha[0]\">$montha[1]</option>";

                    }

                  }

                ?>

              </select> 

            </td>

          </tr>

          <tr>

            <td colspan="2" class="search"><button type="submit" name="submit" style="width:100%">Search</button></td>

          </tr>

        </table>

      </form>

      <br />

      <br />

      <?php if (isset($_GET['submit']) && sizeof($error) == 0): ?>

      <table border="1"> 

        <tr>

          <th>Amount</th>

          <th>Sum, LT</th>

        </tr>

        <?php

          foreach ($infos as $info)

          {

            echo "<tr>";

            echo "<td>$info[0]</td>";

            echo "<td>$info[1]</td>";      

            echo "</tr>";

          }

          if (count($infos) == 0)

          {

            echo "<tr><td colspan=\"2\">Rezultatų nėra.</td></tr>";        

          }

        ?>

      </table>  

      <?php endif; ?>

      <br/>

      <br/>

      <br/>

      <br/>

    </div>

<?php include_once 'footer.php' ?>  

希望有人能理解我的实际需要,因为我只是卡住了,不知道为什么这个页面不会输出任何东西。谢谢。

1 个答案:

答案 0 :(得分:2)

在查询中,您可以使用表格列中的月份和年份,如下所示。

 Month(parking_time) = $month   and YEAR(parking_time) = $year

现在,在您创建查询的文件中添加这些,而不是使用like。

进行比较