Jump to content

Display unique values


Go to solution Solved by 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>
Edited by Makke_
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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.