2012-09-10 7 views
6

voglio recuperare alcuni tag dal mio database, sono nella forma:come selezionare le parole chiave uniche da un separati da virgola tag

topic_id  tags 
    1  `tag1,tag2,tag3` 
    2  `tag1,tag4,tag5` 
    3  `tag2,tag4,tag5` 
    4  `tag6,tag7,tag2` 

voglio avere qualcosa di simile:

tag1 tag2 tag3 tag4 tag5 tag6 tag7 

ie tutti i tag univoci

In modo che sia possibile racchiudere ogni tag in un collegamento per raggruppare articoli di notizie con tag specifici.

Questa seguente query che ho scritto finora non funziona:

$tags = mysql_query("SELECT tags, topic_id 
         FROM forum_topics 
         WHERE topic_id > 0") or die (mysql_error()); 
        while($tag = mysql_fetch_assoc($tags)){ 
        $split_tags = "$tag"; 
        $pieces = explode(",", $split_tags); 
        echo $pieces ; 

Quando ho fatto print_r($pieces);

ho avuto Array ([0] => Array) Array ([0] => Array) Array ([0] => Array) Array ([0] => Array)

che non era quello che stavo cercando.

Come è ora la mia struttura di tabella è simile a questa topic_id , topic_head, topic_body, topic_tag, topic_date, topic_owner .. Come posso rendere ulteriormente topic_tag normale.

+2

È un tavolo che hai costruito? Se è così, è chiaramente non normalizzato. Una tabella normalizzata avrebbe una mappatura 1-a-1 tra topic_id e i tag. Potresti quindi semplicemente eseguire una query 'select distinct tags from forum_topics' per ottenere ciò che desideri. –

+5

Inoltre, non utilizzare le funzioni 'mysql_ *' nel codice. Queste funzioni non sono più mantenute e [sono deprecate] (http://news.php.net/php.internals/53799). Invece, dovresti usare [MySQLi] (http://php.net/mysqli) o [PDO] (http://php.net/pdo). Non sai quale usare? [Questo articolo] (http://php.net/manual/en/mysqlinfo.api.choosing.php) dovrebbe aiutare. –

risposta

3

Se normalizzare la struttura del database, allora si potrebbe ottenere tutti i tag distinti molto facile da

SELECT DISTINCT tags FROM forum_topics WHERE topic_id > 0 

Ma ora, con la struttura del database, non è possibile fare questo, è necessario ottenere tutti i tag e utilizzare array_unique su di loro.

$tags = array(); 
$rows = mysql_query("SELECT tags FROM forum_topics WHERE topic_id > 0") or die (mysql_error()); 
while($row = mysql_fetch_assoc($rows)){ 
    $tags = array_merge($tags, explode(',' $row['tags'])); 
} 
$tags = array_unique($tags); 
print_r($tags); 

Ma anche si possa fare ciò, normalizzare la struttura del database è la scelta migliore.

+0

Grazie per la risposta. Come è ora la mia struttura di tabella è simile a questo 'topic_id, topic_head, topic_body, topic_tag, topic_date, topic_owner' .. Come posso rendere ulteriormente topic_tag normale. –

+1

@DotOyes Crea un'altra tabella 'topic_tags' per gestire la relazione di argomenti e tag. – xdazz

2

Prova questo:

$tags = ""; 
while($row = mysql_fetch_assoc($tags)) { 
    $tags .= $row["tags"] . ","; 
} 

$tags = rtrim($tags, ","); 
$pieces = explode(",", $tags); 

print_r($pieces); // all of them 

$pieces = array_unique($pieces); 

print_r($pieces); // distinct 

... e come Jonah Bishop already mentioned, si prega di evitare mysql_* funzioni.

0
select distinct tags from forum_topics; 
+1

Non sono sicuro al 100% che ciò comporterà ciò che l'OP vuole. Il suo tavolo non è normalizzato, quindi potrai (potenzialmente) ottenere tanti risultati quante sono le voci. –

+0

Ah, beh, questo complica le cose. (E scusa se non ho visto il tuo commento). –

Problemi correlati