Jump to content

[SOLVED] Pulling tags out of a database


emma57573

Recommended Posts

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

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.

 

 

Ive worked this out yay  ;D

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.