1. Requirements Description
The first page of the article list, need to get the point of the article like and is the number of attention, and if the current user likes or attention will show the corresponding small icon to distinguish. This is illustrated below:
2. Solution
Database design:
The article corresponds to the article table, which includes the collection number field fav, likes the number field Zan
The intermediate table User_fav,user_zan represents a user's likes and favorites for an article, including fields such as userid,usertype,article_id.
TP5 Code Implementation section:
TP5 for frames, referring to the development manual, the associated model section, adding Userfav and Userzan methods to the article model
Public function userfav () { return$this->hasmany (' userfav ', ' article_id ')->field (' userid '); } Public function Userzan () { return$this->hasmany (' Userzan ', ' article_id ')->field (' userid '); }
The index method in the controller displays the list of articles
Public functionIndex$page=1) { //$this->articlemodel = new article (); $list = $this->articlemodel->where (' SchoolID ', $user [' SchoolID '])->where (' ClassID ', $user [' ClassID '])- >group (' Create_time ')->select (); $list=$this->articlemodel->withcount ([' Userzan ' =function($query){ $query->where (' userid ', Cookie (' userid '))->where (' usertype ', Cookie (' usertype '))); }])->order (' Zan desc,is_top desc,is_recommend desc,id desc ')->paginate (5,false, [' page ' = =$page]); $this->assign (' list ',$list); return $this-fetch (); }
View to get the number of points of interest in the same time to judge if the current user likes, part of the code
{volist name= "list" id= "Vo"} <divclass= "In_parent" > $vo[' Photo '] [0]} " Data-id= "{$vo[' ID ']} " > <divclass= "In_par_con" > <p>{$vo.title}</p> </div> <divclass= "In_par_con" > {foreach:Explode(‘,‘,$vo[' tags ']) as $tag} <span>{$tag}</span> {/foreach} </div> <divclass= "In_per" > <divclass= "In_per_lf fl" > class= "FL"/> <divclass= "fl ml5 mt10" >{$vo. author}<pclass= "Gray" >{php}Echo Date(' y-m-d ',Strtotime($vo[' Create_time ']); {/php}</p></div> </div> <divclass= "In_per_rt fr" > <spanclass= "Gray fr Mt30 ml3 care-val" > Favorites {$vo.fav}</span> <divclass= "Fr Mt30 ml3 care{eq name= '$vo. Userfav_count ' value= ' 1 '} Care01{/eq} ' data-id= ' {$vo. ID} "></div> <spanclass= "Gray fr Mt30 ml3 zan-val" >{$vo.zan}</span> <divclass= "Fr Mt30 ml3 zan{eq name= '$vo. Userzan_count ' value= ' 1 '} Zan01{/eq} ' data-id= ' {$vo. ID} "></div> </div> <divclass= "Clear" ></div> </div> </div> {/volist}
PHP is not used for a long time, use the wrong place to hope to enlighten
Thinkphp Association Model using