TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-08-2008, 12:47 PM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default Howto limit same results to one?

Hi all!

How do I limit same results to as one?!

example: if my query fetch like this:

Code:
Banana
Banana
Banana
Apple
Tomato
Kiwi
Kiwi
Kiwi
Kiwi
Kiwi
Kiwi
Onion
Onion
I need to limit those results as below:
Code:
Banana
Apple
Tomato
Kiwi
Onion
Like that.. To limit multiple results as one! =)

Thanks for all help!
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 03-08-2008, 01:18 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Are you fetching the list from a database? If so, there are a couple of options you can use.

The first is to use the DISTINCT keyword in your query:

SQL Code:
SELECT DISTINCT fruit FROM products

The second is to use the GROUP BY clause in your query:

SQL Code:
SELECT fruit FROM products GROUP BY fruit

Both will give you the desired result

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 03-08-2008, 01:55 PM   #3 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default

Hi Alan and thanks 4 replying.
Yes I'm fetching these from database (sorry for not metion that ;))

Amm.. What if I don't know which results will be multiple at the time I'm coding this?

Or then I didn't quite understand your examples?
Well let's bake one example this way.

I have news-script where is several posts for each month.
I want to create archive links for each those months, automaticly ofcourse..

I would do it something like this:
PHP Code:
$q mysql_query("SELECT added FROM news ORDER BY added DESC") or die(mysql_error());
while(
$q1 mysql_fetch_array($q)) {

$timestamp $q1['added'];
$datetime date("F Y" strtotime("$timestamp"));
$year date("Y"strtotime("$timestamp"));
$month date("m"strtotime("$timestamp"));

print(
"<li><a href=\"/news/".$year."/".$month."/\">".$datetime."</a></li>\n");

Okey, works but if I have 7 news for march then I have 7 March 2007 links over there.. Now, those I must put as one.

Maybe your example Alan works but I have now clue how I should do it?

Thanks for all help! =)
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 03-08-2008, 02:12 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

In your example above I would move the unique check to the PHP code.

For example, instead of print()'ing your date links, I would add them to an array and then use array_unique() on it to strip out duplicates.

Something like the following (untested) should work:

PHP Code:
$q mysql_query("SELECT added FROM news ORDER BY added DESC") or die(mysql_error()); 
while(
$q1 mysql_fetch_array($q)) { 

$timestamp $q1['added']; 
$datetime date("F Y" strtotime("$timestamp")); 
$year date("Y"strtotime("$timestamp")); 
$month date("m"strtotime("$timestamp")); 

$dates[] = "<li><a href=\"/news/".$year."/".$month."/\">".$datetime."</a></li>\n";
}

$dates array_unique($dates);
foreach (
$dates as $datelink)
{
echo 
$datelink;

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
marxx (03-08-2008)
Old 03-08-2008, 02:17 PM   #5 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default

Whau.. You are gooood Alan!! =)

Thank You!
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 03-08-2008, 02:20 PM   #6 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Glad I could help

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 03-08-2008, 06:50 PM   #7 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

If the type of the added column is DATE or DATETIME you can do it with a simple query:
SQL Code:
SELECT DISTINCT MONTHNAME(`added`) AS `month`, YEAR(`added`) AS `year` FROM `news` ORDER BY `year` DESC, `month` DESC

This will return a table like this:
Code:
+--------+  +------+
| month  |  | year |
+--------+  +------+
|March   |  | 2008 |
|February|  | 2008 |
|January |  | 2008 |
|December|  | 2007 |
+--------+  +------+
Now to display the list you simply do:
PHP Code:
while ($q1 mysql_fetch_array($q)) {
  echo 
$q1['month'] . "/" $q1['year'];

You can also increment that query to ignore the current month/year and to limit the number of results (let's say you want only 5 links):
SQL Code:
SELECT DISTINCT MONTHNAME(`added`) AS `month`, YEAR(`added`) AS `year` FROM `news` WHERE MONTH(`added`) != MONTH(NOW()) AND YEAR(`added`) != YEAR(NOW()) ORDER BY `year` DESC, `month` DESC LIMIT 5
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:37 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design