1. The structure of the data table:
Create two data tables, structured as follows:
Where the tag field in the Mood table, the ID field of the tag table + "," +tag the ID field of the table,
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.
$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