Reputation: 63
Currently I retrieving DATE records like "01/05/2014" and "01/06/2014" from database and display into select option.
Here my code:
<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>
The output of $year I get will be 2014, 2014 which is duplicated. How can I only display only one 2014 in my dropdown list?
Upvotes: 0
Views: 830
Reputation: 4089
try this
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>';
}
Upvotes: 0
Reputation: 8411
You can get distinct year like this,
SELECT DISTINCT YEAR(STR_TO_DATE(photo_album_date,'%d/%m/%Y')) FROM tblPhotoAlbum
(Not tested)
Upvotes: 2