目前,我从数据库中检索“01/05/2014”和“01/06/2014”等DATE记录,并显示为选择选项。
这是我的代码:
<label>Year</label>
<select style="margin-right:20px;">
<option value="--" selected="selected">--</option>
<?php
require('Retail/database_connection.php');
if (@mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_album_date= "SELECT DISTINCT photo_album_date FROM tblPhotoAlbum";
$result = mysqli_query ($mydatabase, $select_album_date);
if($result)
{
while($row = mysqli_fetch_array($result))
{
$date = $row['photo_album_date'];
$arr = explode("/", $date);
//Split date result into $day, $month, and $year
list($month, $day, $year) = $arr;
echo '<option>'.$year.'</option>';
}
}mysqli_close($mydatabase);
?>
</select>
我获得的每年的产出将是2014年,2014年是重复的。如何才能在下拉列表中仅显示一个2014?
答案 0 :(得分:2)
你可以像这样获得截然不同的年份,
SELECT DISTINCT YEAR(STR_TO_DATE(photo_album_date,'%d/%m/%Y')) FROM tblPhotoAlbum
(未经测试)
答案 1 :(得分:0)
试试这个
while($row = mysqli_fetch_array($result))
{
$date = $row['photo_album_date'];
$arr = explode("/", $date);
//Split date result into $day, $month, and $year
list($month, $day, $year) = $arr;
// save year into an array
$yr[] = $year;
// echo only if year is not in array
if(!in_array($year,$yr))
echo '<option>'.$year.'</option>';
}