This paper describes the method of Yii to implement the top-of-list feature of the article. Share to everyone for your reference, as follows:
My understanding: First click to get the current Id,model layer query all sort fields, traverse the data, get the maximum value, modify the data, replace the data.
:
Model layer:
Display list public Function lists1 () { $arr =yii:: $app->db->createcommand ("SELECT * to Acticle join type on Type.t_ id=acticle.t_id ORDER BY sort desc ")->queryall (); return $arr;} Pinned Public function top () { $arr = $this:: Find ()->select ("Sort")->asarray ()->all (); Print_r ($arr);d ie; $rows =array (); foreach ($arr as $key = + $v) { $rows []= $v [' sort ']; } $max =array_search (Max ($rows), $rows); return Intval ($rows [$max]+1); } Modify data public function update1 ($sort, $acticle _id) { $arr =yii:: $app->db->createcommand () Update ("Acticle", [' sort ' = ' $sort],[' acticle_id ' = ' + $acticle _id]); if ($arr->execute ()) { return 1; } else{ return 2;} }
Controller:
Article pinned Public function Actiontopq () { $acticle _id=$_get[' id ']; echo $acticle _id;die; Gets the maximum sort $model =new acticle (); $sort = $model->top (); Modify data $row = $model->update1 ($sort, $acticle _id); echo $row;d ie; if ($row ==1) { //replace data, pinned $res 1= $model->lists1 (); $art =new articles (); $res 6= $art->get_right ($res 1,5); return $res 6; } else{ return false; }}
View Layer:
<!--article List--><div class= "R_230_b ma_b8" style= "Float:right;right:0;line" ><div class= "news_t" >< H2><font color= "#d52c99" > Latest news </font>
It is hoped that this article is helpful to the PHP program design based on YII framework. Thanks for the support of topic.alibabacloud.com.