开发者

Select unique text from an array

开发者 https://www.devze.com 2023-04-04 01:59 出处:网络
I am having a problem selecting the unique text from database. This is for a 开发者_运维技巧movie database, Let\'s assume I have 2 movies in the database.

I am having a problem selecting the unique text from database.

This is for a 开发者_运维技巧movie database, Let's assume I have 2 movies in the database.

1st movie is in category: Drama, Romance, War 2nd movie is in category: Drama, Thriller

What I need, is to return an array wich will display me: Drama, Romance, War, Thriller. My current query to mysql is the following:

$query = 'SELECT genres FROM imdb WHERE actors LIKE "%%'.$name.'%"';

In an while loop I use "foreach", like this:

foreach(explode(', ', $TMPL['genres']) as $v)
                    $TMPL['genre'] .= '<option value="'.urlencode($v).'">'.($v).'</option>';

This is returning me all the values, including the duplicated one, like this: Drama, Romance, WarDrama,Thriller

Any clue how to sort this out?


$query = 'SELECT DISTINCT genres FROM imdb WHERE actors LIKE "%%'.$name.'%"';

would do it


just use the array_unique function on that array of your with duplicate values!

$unique_values = array_unique($TMPL['genre']);


use DISTINCT

$query = 'SELECT DISTINCT genres FROM imdb WHERE actors LIKE "%%'.$name.'%"';


So you have a column with a delimited list of genres?

Loop through every row in your query and explode the genres, add every genre to a master array and then use array_unique()

If i understand you correctly you wont be able to do it (easily) with MySQL as you haven't normalised your database properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消