> I have a keywords field in a mysql database - allows up to 100
> characters of words seperated by spaces.
> 
> at a late stage in the development process the client would now like a
> summary list of keywords that they've entered.
> 
> I guess what I need to do is open each keyword field, extract the
words,
> add them to an array, remove duplicates and sort alphabetically.
> 
> sounds easy but I'm still finding my way - anyone got a sample script
or
> some helpful hints they'd care to share?

How about just fixing this so it'll be easier in the future? You should
have a separate table where each "keyword" is in it's own row, with or
without duplicates (depends if you want a "count" of how often the
keyword is used), and an ID relating back to the "article" or whatever
you have. 

The process of creating the table and creating the array are about the
same. The table will give you more flexibility, though, when the client
starts asking for more "stats". 

To use the array method, like you originally asked:

$var = array();
$query = "SELECT keyword FROM table";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{ 
  $ar = explode(' ',$row['keyword']);
  array_merge($var,$ar); 
}
$var = array_unique($var);
$final_array = sort($var);

To INSERT everything into a second table:

$query = "SELECT keyword FROM table";
$rs = mysql_query($query);
while($row = mysql_fetch_assoc($rs))
{
  $keywords = explode(' ',$row['keyword']);
  $insert_string = "'" . implode("','",$keywords) . "'";
  //assumes no ' characters in $keywords
  $query = "INSERT INTO keywords_table VALUES ($insert_string)";
  $rs2 = mysql_query($query);
}
Make the keywords column UNIQUE if you don't want duplicates, although
it would probably be better to just leave duplicates in there and do a
SELECT DISTINCT to remove them. Leaving them in there will allow you to
produce more stats on which keywords are most popular, etc. 

---John W. Holmes...

Amazon Wishlist: http://www.amazon.com/o/registry/3BEXC84AB3A5E

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to