PHP Tag Cloud production process (data table structure and query)

Source: Internet
Author: User
Tags explode php code tagname
1. The structure of the data table:

Create two data tables, structured as follows:

Tag Tag table:

Article Mood table:

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

2. Query method:

For example, if you need an article that contains a tagname with ID 1,2,3 in the tag table, which is the label ID 1,2,3,

When you add an article, you use the

$result =implode (",", $_post[' TagID '))//The array of fetched checkbox is separated by 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 the writing of thinkphp, the original writing is very similar

When you store the article, you only need to mood the tag= $result in the table.

The data has been saved, and the next thing we need to do is to click on the appropriate tab to query all the articles that contain the tag.

If we need to display all the tags contained in an article, we need to get the ID of the article and query the tag for that article.

Using the Split function

$taglist = Explode (', ', $source); $source is the tag value for the article, for example: dividing tag= "1,2,3" into an array

Then at the front desk you can write:

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 ";
}

Loop output Tagname,url tag table ID value, next only need to receive the URL value of the place to write a fuzzy query SQL, the article table of the tag like%id%.

Note: The above query statements are all thinkphp syntax.

This can be a problem with a fuzzy query because, for example, one of the tag fields in the article table might contain 1, 5 another tag field might 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,% is not good.

So here I write the PHP code in the foreground, with two nested for loops 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 passed TagID exists in the tag field of the article, output.
Dump ($arr _mood[$a] [' title '])//This can be output to the foreground with Echo
}

}

}

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

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.