Searching and displaying distinct words

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Searching and displaying distinct words

Post by hame22 »

Hi,

I have records for products which include a field for the products keywords.

What I want to do is display a list of all disticnt keywords held within this field.

Any ideas as to how i can do this?

thanks in advance
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

select distinct field_name from table_name
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

yes but in my keyword field I have values such as keyword1, keyword 2, keyword 3

i need to break these down first and then select distinct values from the individual keywords.

Can this be done?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

hame22 wrote:yes but in my keyword field I have values such as keyword1, keyword 2, keyword 3

i need to break these down first and then select distinct values from the individual keywords.

Can this be done?
Ah I see... comma separated.

I think this is out of mysql's hands now so you'll need to use php:

Code: Select all

$query = "select field_name from table_name";
$result = mysql_query($query);
$out = array();
while ($row = mysql_fetch_assoc($result))
{
    $parts = explode(',', $row['field_name']);
    $out = array_merge($out, $parts);
}
$out = array_unique($out);

print_r($out);
If there's a way to do it with mysql it'll be a bit loopy :P

EDIT | Passed array_unique output back to $out (I thought it was done by-reference)
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

that looks cool,

what i then wanted to do is for each keyword, make it into a hyperlink so would be displaying:

<a href>Keyword</a>

Can your script be adapted to do this?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

hame22 wrote:that looks cool,

what i then wanted to do is for each keyword, make it into a hyperlink so would be displaying:

<a href>Keyword</a>

Can your script be adapted to do this?
Just use a foreach on the resultant array:

Code: Select all

$query = "select field_name from table_name";
$result = mysql_query($query);
$out = array();
while ($row = mysql_fetch_assoc($result))
{
    $parts = explode(',', $row['field_name']);
    $out = array_merge($out, $parts);
}
$out = array_unique($out);

foreach ($out as $keyword)
{
    echo "<a href="#">$keyword</a><br />\n";
}
Post Reply