Re: condensing a list from MySQL

From: Date: Mon, 09 Jun 2003 19:08:39 +0000
Subject: Re: condensing a list from MySQL
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-150791@lists.php.net to get a copy of this message
> 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. I think, that the problem with duplicates can be solved by using "distinct" parametr in SQL-query whitch count each keyword only once, however it is there ten times... Pavel > > 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/ > >

« previous php.general (#150791) next »