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
Searching and displaying distinct words
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
select distinct field_name from table_name- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Ah I see... comma separated.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?
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);EDIT | Passed array_unique output back to $out (I thought it was done by-reference)
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Just use a foreach on the resultant array: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?
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";
}