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.
È 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. –
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. –