如何根据年份动态回显月份

时间:2015-11-29 17:07:57

标签: php mysql select

我有以下代码

<?php
    echo '<div class="row">
            <div  id="year" class="col-md-3 ">
                <select id="year1" class = "form-control" name="year" onblur="sctMonth()">
                   <option id="1" value="" disabled selected>-- Year --</option>';

    $year_query = "SELECT  DISTINCT YEAR (uploaddate) AS OrderYear FROM billTable  WHERE username='$log_username' ORDER BY uploaddate ASC ";
    $result_year_query = mysqli_query($db_conx, $year_query);

    echo $e2= mysqli_error($db_conx );
    while ( $year_query = mysqli_fetch_row($result_year_query)){
        $year_query = $year_query[0];
        echo '<option value='.$year_query.'>'.$year_query.'</option>';
    }
    echo '      </select>
            </div>';

    echo '<div  id="month" class="col-md-3 ">
            <select id="month1" class = "form-control" name="month" >
               <option id="1" value="" disabled selected>-- Month --</option>';

    if(isset($_POST["monthcheck"])) {
        include_once("php_includes/db_conx.php");
        $year = preg_replace('#[^a-z0-9]#i', '', $_POST["monthcheck"]);//year options for month
        $month_query = "SELECT DISTINCT MONTHNAME(uploaddate) AS OrderMONTH 
                        FROM billTable 
                        WHERE username='$log_username' AND year(uploaddate) = '$year' 
                        ORDER BY uploaddate ASC ";
        $result_month_query= mysqli_query($db_conx, $month_query);
        while ( $month_query = mysqli_fetch_row($result_month_query)){
            $month_query = $month_query[0];
            //echo json_encode($month_query);
            echo '<option value='.$month_query.'>'.$month_query.'</option>';
        }
    }
    echo '      </select>
            </div>
            <span id="month1_status"></span></div>';
            //echo $year ;
?>

当用户选择年份,月份时应该使用ajax显示我想要做的事情,我使用ajax将年份发送到php,但我无法弄清楚如何显示月份和刷新年份变化,目前它只显示空白的选择框,或者使用json_encode($month_query);将月份作为数组传递更好,但我不知道如何使用它。

1 个答案:

答案 0 :(得分:0)

不完全是你想要的,但代码是完全不可读的。我建议你写一下更具可读性,因为你以后会感谢自己。

我认为错过了一段代码并更好地解释了他的错误

顺便说一句。这是一个更易阅读的版本

<?php

$log_username = mysqli_real_escape_string($db_conx, $log_username);
$year_query = <<<SQL
  SELECT DISTINCT YEAR(uploaddate) AS OrderYear
    FROM billTable
  WHERE username = "{$log_username}"
  ORDER BY uploaddate ASC  
SQL;

$query_year_result = mysqli_query($db_conx, $year_query);
$query_year_error = mysqli_error($db_conx);

$select_block = <<<HTML
<div class="row">
  <div id="year" class="col-md-3">
    <select id="year1" class="form-controler" name="year" onblur="sctMonth()">
      <option id="1" value="" disabled selected>-- Year --</option>
      YEAR_OPTIONS
    </select>
  </div>
  <div id="month" class="col-md-3">
    <select id="month1" class="form-controler" name="month">
      <option id="1" value="" disabled selected>-- Month --</option>
      MONTH_OPTIONS
    </select>
  </div>
  <span id="month1_status"></span></div>
</div>
HTML;

$year_options = "";

if (!$query_year_error) {
  while($row_year_query = mysqli_fetch_row($query_year_result)) {
    $year = $row_year_query[0];
    $year_options .= "<option value="{$year}"></option>";
  }
}

$month_options = "";

if (isset($_POST['monthcheck'])) {
  // I'll left this here commented because I dont know why are you calling here
  // if you use $qb_conx before  
  // include_once("php_includes/db_conx.php");

  $year = preg_replace('#[^a-z0-9]#i', '', $_POST["monthcheck"]);
  $year = mysqli_real_escape_string($db_conx, $year);

  $month_query = <<<SQL
    SELECT DISTINCT MONTHNAME(uploaddate) AS OrderMONTH 
      FROM billTable 
    WHERE username = "{$log_username}" AND year(uploaddate) = "{$year}"
    ORDER BY uploaddate ASC 
  SQL;

  $query_month_result = mysqli_query($db_conx, $month_query);
  $query_month_error = mysqli_error($db_conx);  

  if (!$query_month_result) {
    while($row_month_query = mysqli_fetch_row($query_month_result)) {
      $month = $row_month_query[0];
      $month_options .= "<option value="{$month}">{$month}</option>";
    }
  }
}
$select_block = str_replace('YEAR_OPTIONS', $year_options, $select_block);
$select_block = str_replace('MONTH_OPTIONS', $month_options, $select_block);

echo $select_block;