PHP Tag Cloud Production--data table structure and query method

Source: Internet
Author: User
Tags explode tagname

1. Structure of the data sheet:

Create two data tables with the following structure:

Label Tag table:

Article Mood table:

Where the tag field in the Mood table is the ID field of the tag table + "," The ID field of the +tag table,

2. Query method:

For example, if you need an article that contains a tagname with the ID of *. A in the tag table, which is the tag with the ID of

When you add an article, use the

$result=implode$_post[' TagID ']); // divide the array of captured checkboxes with commas
$_post[' TagID ' is an array of check boxes for the foreground, and the foreground HTML section code is:
<input type= "checkbox" Name= "tagid[" "value=" {$tag. ID} "id=" {$tag. ID} "><label for=" {$tag. ID} ">{$ tag.tagname}</label>//here is Thinkphp's writing, the original writing is similar

When storing the article, only the tag= $result in the mood table is required.

The data has been saved, and what we need to do next is to click on the corresponding tag to find out all the articles that contain the tag.

If we need to display all the tags contained in an article, we must first obtain the ID of the article, and query the tag of the article,

Using the Split function

$taglist Explode (', ',$source); The $source is the tag value of the article, for example: dividing the tag= "one-hand" into an array

It can then be written at the front desk:

 for ($index= 0; $index<count($taglist); $index+ +)             {$tagsa=$tagdata->where (' id=%d ',$taglist[  $index]), select ();             Echo "<a id= ' tag ' href= ' location/tag/". $tagsa [0] [' ID ']. "' > ". ($tagsa[0][' tagname ']). " </a>&nbsp&nbsp&nbsp " ;              }

The loop output Tagname,url the ID value of the tag table, and then only need to write a fuzzy query in the location of the URL value of the SQL, the article table of the tag like%id%.

Note: The above query statements are thinkphp syntax.

There is a problem with fuzzy queries, because for example: one of the tag fields in the article table may contain 1, 5 another tag field may contain 10, 23

If the query tag like%1% will query out the tag field 1,5 and tag field 10,23 two articles. Even if the like condition is%1,% or%,1,%, it is not possible.

So here my writing is to write PHP code in the foreground, with two nested for loop to solve, as follows:

$map[' tag '] =Array(' like ', '% '. TagID. ' %‘); //dump ($selecttag [$i] [' id ']);$arr _mood=$mood->where ($map),Select (); for($a= 0;$a<Count($arr _mood);$a++){                $source=$arr _mood[$a[' Tag ']; $taglist=Explode(‘,‘,$source);  for($index= 0;$index<Count($taglist);$index++){                                         if(tagid==$taglist[$index] {//When the TagID is present in the tag field of the article, the output. Dump ($arr _mood[$a[' title ']);//Here you can use echo to output to the foreground}} }

Since the blogger is a novice php, so the code has a rough place, please forgive me!

If you have a better way, you may as well tell me ' (*∩_∩*)

PHP Tag Cloud Production--data table structure and query method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.