Thinkphp Association Model using

Source: Internet
Author: User

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

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.