emma57573 Posted November 29, 2008 Share Posted November 29, 2008 Hi I have the column 'tag' in my database which contains tags for an items eg: tag1,tag2,tag3 I want to output the tags in a php page so they show up in a side menu as links that I can run a search on. Now what I want to do is loop through the tags and each loop I need to separate the tags by the comma so I can handle each word separately. I also need to throw out any duplicates and sort by the most common tags at the top. So far Ive set up a while loop that searches the rows but that's as far as Ive got! I'm not sure how to handle the rest. Has anyone got any idea hows best to handle the rest. I especially need to know how to separate the tags by commas and place in an array that I can loop. I think I would be ok with the rest if I could manage that part. Link to comment https://forums.phpfreaks.com/topic/134732-solved-pulling-tags-out-of-a-database/ Share on other sites More sharing options...
emma57573 Posted November 29, 2008 Author Share Posted November 29, 2008 This is what I have so far <? $tags="select * from item_table where tags!='null' "; $tagloop=mysql_query($tags); $num=20; $no_of_cols=10; $cnt=1; while(($rs_t=mysql_fetch_array($tagloop))&&($cnt<=$num)) { ?> <tr> <td class="tag"><a href="link" class="tag"><? echo $rs_t["tags"];?></a></td> </tr> <? $cnt=$cnt+1; $cnt++; }//end while ?> That outputs the tags as a string but does not seperate them by the comma So instead of <tr> <td class="tag"><a href="link" class="tag"><? echo $rs_t["tags"];?></a></td> </tr> I need to add the tags into an array 'seperated by the comma (which is what im stuck on) and them do another loop to output the array with the above output. Link to comment https://forums.phpfreaks.com/topic/134732-solved-pulling-tags-out-of-a-database/#findComment-701582 Share on other sites More sharing options...
emma57573 Posted November 29, 2008 Author Share Posted November 29, 2008 Ive worked this out yay All I need to do now is stop the tags from duplicating if there is more the one in the array. Any ideas?? This is my code <? $lsql="select * from My_Table where tags!='null'"; $latest=mysql_query($lsql); $num=40; $cnt=1; while(($rst=mysql_fetch_array($latest))&&($cnt<=$num)) { $tagarray= explode(",", $rst["tags"]); for($i=0;$i<count($tagarray);$i++){ $taggy[]=$tagarray[$i]; }//end while ?> <? $cnt=$cnt+1; $cnt++; }//end while for($i=0;$i<count($taggy);$i++){ echo "<tr><td class=\"tag\"><a href=\"link\" class=\"tag\">".$taggy[$i]."</a></td></tr>"; }//end while ?> Works perfectly apart from handling duplicate tags Link to comment https://forums.phpfreaks.com/topic/134732-solved-pulling-tags-out-of-a-database/#findComment-701617 Share on other sites More sharing options...
Garethp Posted November 29, 2008 Share Posted November 29, 2008 When adding it, add a foreach, saying loop through the array and check if it already exists, if not, add to it. Also, use array_push to add it instead Link to comment https://forums.phpfreaks.com/topic/134732-solved-pulling-tags-out-of-a-database/#findComment-701619 Share on other sites More sharing options...
emma57573 Posted November 29, 2008 Author Share Posted November 29, 2008 Thank you I will give it a try Link to comment https://forums.phpfreaks.com/topic/134732-solved-pulling-tags-out-of-a-database/#findComment-701624 Share on other sites More sharing options...
Garethp Posted November 29, 2008 Share Posted November 29, 2008 Also, on the SQL query, use LIMIT 0, 40 at the end instead of the $count and $num condition Link to comment https://forums.phpfreaks.com/topic/134732-solved-pulling-tags-out-of-a-database/#findComment-701626 Share on other sites More sharing options...
emma57573 Posted November 29, 2008 Author Share Posted November 29, 2008 Thanks for your help Its now working And I have taken your advice of the syntax. Link to comment https://forums.phpfreaks.com/topic/134732-solved-pulling-tags-out-of-a-database/#findComment-701630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.