In thinkphp, write the tag frequency statistic algorithm for many-to-many relationships in post tags

Source: Internet
Author: User
Tags tag name

Often see other people's blog inside, or the site has such a random color, but the font size and the frequency of the label is related to the label cloud, so you want to write one. As for the color of the random display, it is very simple, this is not listed in the code.

Because you are learning thinkphp, data lookup is the use of TP data lookup statements.

1          Public functionRender$data ){2             //Find all the labels in the middle table of the post-tag many-to-many relationship3             $tags= M (' blog_tags ')Select ();4             $a=Array();5             //Remove duplicate labels, save all non-repeating labels in array a6              for($i= 0;$i<Count($tags);$i++) {7                 if( !In_array($tags[$i[' Tid '],$a ) ){8                     $a[$i] =$tags[$i[' Tid '];9                 }Ten             } One             //Find all the post IDs under these tags and save them in the array bid A             foreach($a  as $v) { -                 $bid[$v] = M (' blog_tags ')->where (Array(' tid ' =$v))->field (' bid ')Select (); -             } the             foreach($bid  as $v) { -                 $blog _id[] =$v; -             } -             //p ($a); + //P ($blog _id);d ie; -             $b=$a;//Array B is used to hold the original array A, and the number of occurrences of the array a statistic label +             foreach($a  as $k=$v) { A                 $a[$k] =Array(); at                  for($j= 0;$j<Count($tags);$j++ ){ -                     if($v==$tags[$j[' Tid '] ) -                         $a[$k][0] + = 1; -                 } -             } -             $i= 0; in             //combine tag ID, count count, tag name, post ID, into an array of C -             foreach($b  as $k=$v ){ to                 $name= M (' tags ')->where (Array(' id ' = =$v))->getfield (' name '); +                 $c[$i] =Array(' id ' = =$v, ' num ' =$a[$k][0], ' name ' = =$name, ' bid ' =$blog _id[$i] ); -                 $i++; the             } *             //p ($c);d ie; $ Panax Notoginseng             $data[' tags '] =$c; -             return $this->renderfile ("',$data); the}

In thinkphp, write the tag frequency statistic algorithm for many-to-many relationships in post tags

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.