从三个下拉列表中的数据库中检索日期

时间:2013-05-14 20:55:37

标签: php list date drop-down-menu

我需要在三个下拉列表中从数据库中检索日期。 Mysql& PHP代码:

if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
  $sql = "SELECT ui.`sex` AS ui_sex , 
  ui.`last_name` AS ui_lname, 
  ui.`first_name` AS ui_fname, 
  ui.`nickname` AS ui_nickname, 
  ui.`nin` AS ui_nin, 
  ui.`birth_date` AS ui_birth,             
  WHERE ui.`userid` = 1";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
if ($count == 0) {
  echo '<h4>No data submited yet.</h4>';
}
else{
   while ($row = mysqli_fetch_array($result)) {
      $ui_sex = $row['ui_sex'];
      $ui_nume = $row['ui_lname'];
      $ui_prenume = $row['ui_fname'];
      $ui_nickname = $row['ui_nickname'];
      $ui_nin = $row['ui_nin'];
      $ui_birth = $row['ui_birth'];
   }
}

HTML code:

<form action="submit_values.php" method="POST">
<!-- Code for previous fields -->
<label class='small-3 columns label_inf_clients'>Day of birth*:</label>
        <div class="small-6 columns select_tag_bottom_margin"> 
            <select id="select_day" name="day" >
    <?php
        //the next option should store the day from $birth_date
         echo'<option class="option_an" value='.$day.'>'.$day.'</option>';

    ?>
    <?php
    for ($i = 1; $i <= 31; $i++) {
        echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
    }
    ?>
            </select>

       <select class="select_month" name="month">
            <?php
               //the next option should store the month from $birth_date
              echo'<option class="option_an" value='.$month.'>'.$month.'</option>';
            ?>
            <option value="01">Jan</option>
             .............................
            <option value="11">Nov</option>
            <option value="12">Dec</option>
        </select>
        <select class="select_year" name="an">
    <?php
        //the next option should store the year from $birth_date
              echo'<option class="option_an" value='.$year.'>'.$year.'</option>';
    for ($i = 2013; $i >= 1940; $i--) {
        echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
    }
    ?>
    </select>
</form>

所以让我们回顾一下:从数据库中选择出生日期,存储在$ ui_birth中,我希望将此日期拆分为3个下拉列表,如下图所示:https://docs.google.com/file/d/0B44T4qmqQGHAY3JkdFZNOGdLTW8/edit?usp=sharing

2 个答案:

答案 0 :(得分:0)

您可以使用php的日期函数提取日期的组件,如下所示:

$ui_birth = $row['ui_birth'];
$birth_date = strtotime($ui_birth);
$day = date('d', $birth_date);
$month = date('m', $birth_date);
$year = date('Y', $birth_date);

答案 1 :(得分:0)

您缺少一些双引号和单引号,而且您没有正确关闭选项代码。

此...

echo'<option class="option_an" value='.$day.'>'.$day.'</option>';

应该是这个......

echo'<option class="option_an" value="'.$day.'">'.$day.'</option>';

此...

for ($i = 1; $i <= 31; $i++) {
    echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
}

应该是这个......

for ($i = 1; $i <= 31; $i++) {
    echo'<option  class="option_an" value="' . $i . '">' . $i . '</option>';
}

此...

echo'<option class="option_an" value='.$month.'>'.$month.'</option>';

应该是这个......

echo'<option class="option_an" value="'.$month.'">'.$month.'</option>';

此...

for($i = 2013; $i >= 1940; $i--) {
    echo'<option  class="option_an" value=' . $i . ">" . $i . '<option>';
}

应该是这个......

for ($i = 2013; $i >= 1940; $i--) {
    echo'<option  class="option_an" value="' . $i . '">' . $i . '</option>';
}

另外你应该将MYSQLI_ASSOC添加到你的mysqli_fetch_array ......

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

此外,您的sql语句应如下所示......

$sql = "SELECT sex AS ui_sex , last_name AS ui_lname, first_name AS ui_fname, nickname AS ui_nickname, nin AS ui_nin, birth_date AS ui_birth FROM ui WHERE ui.`userid` = 1";