Home  >  Article  >  Backend Development  >  PHP tag cloud production - data table structure and query method_PHP tutorial

PHP tag cloud production - data table structure and query method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:511051browse

PHP tag cloud production - data table structure and query method

For example: If you need an article to contain tagnames with ids 1,2,3 in the tag table, that is, tags with ids 1,2,3,
then use
when adding an article
$result=implode(",", $_POST['tagid']);//Split the obtained checkbox array with commas
$_POST['tagid'] is an array to obtain the checkboxes in the frontend. The frontend html part code is:
//This is how thinkphp is written, the native way of writing is similar
When storing articles in this way, you only need tag=$result in the mood table.
The data has been saved. What we need to achieve next is to click on the corresponding tag to query all articles containing that 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,
Use split function
$taglist = explode(',',$source); //$source is the tag value of the article, for example: split tag="1,2,3" into an array
Then you can write like this in the front desk:
for($index=0;$index
$tagsa=$tagdata->where('id=%d',$taglist[$index])->select();
                    echo "".($tagsa[0]['tagname'] )."   " ;
      }
Loop to output the tagname, and the url passes the id value of the tag table. Next, you only need to write a fuzzy query sql where the url value is received, and the tag of the article table is like %id%.
Note: The above query statements are all thinkphp syntax.
There will be a problem when using fuzzy query like this, because for example: one of the tag fields in the article table may contain 1,5 and the other tag field may contain 10,23
If you query tag like %1%, you will find two articles with tag field 1,5 and tag field 10,23. Even if the like condition is %1,% or %,1,%, it will not work.
So my way of writing here is to write php code in the foreground and use two nested for loops to solve the problem, as follows:
Copy code
$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
                                                                             
if (tagid == $ taglist [$ Index]) {// When the tagid that comes over exists in the tag field of the article, the output is output.
                                                                                                                                                                                                                                                             dump($arr_mood[$a]['title']);//Here you can use echo to output to the front desk
          }
                                                                             
        }  
                                                       
                                                       
      }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871180.htmlTechArticlephp tag cloud production - data table structure and query method. For example: if you need a tag table to be included in an article The tagname with id 1,2,3, that is, the tag with id 1,2,3, then add the text...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn