Jump to content

Display unique values


Makke_

Recommended Posts

Hi,

I am trying to display week number in a drop down menu.
I managed to display the numbers but not distinct.
I´ve tried using array_unique but i´m not sure how to implement it. (below code shows how I tried to use array_unique.

 

$sql="SELECT Date FROM maildata ORDER BY Date ASC";
  $result = mysql_query($sql) or die(mysql_error());  
   while($row = mysql_fetch_array($result)){
   $date_string = date($row['Date']);
   $week = array_unique($date_string);
   $options .="<option>". date("W", strtotime($week))."</option>";

   ?>
   <SELECT name="week">
   <OPTION VALUE="">Choose week
   <?=$options?>
   </SELECT>
Link to comment
https://forums.phpfreaks.com/topic/276430-display-unique-values/
Share on other sites

Since $date_string is not an array, that code should report an error.

 

To use array_unique() you would need to collect all of the week numbers in an array, then call it against that array.

 

On the other hand, you should select DISTINCT dates so you don't process the same date multiple times. In fact, you could do the week number and distinct in the query (mySql WEEK):

SELECT DISTINCT WEEK(Date) AS WeekNo FROM maildata ORDER BY 1 ASC

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.