《PHP實(shí)例:Yii實(shí)現(xiàn)文章列表置頂功能示例》要點(diǎn):
本文介紹了PHP實(shí)例:Yii實(shí)現(xiàn)文章列表置頂功能示例,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
本文實(shí)例講述了Yii實(shí)現(xiàn)文章列表置頂功能的方法.分享給大家供大家參考,具體如下:PHP編程
我的理解:首先點(diǎn)擊獲取當(dāng)前ID,model層查詢所有sort字段,遍歷數(shù)據(jù),得到最大值,修改數(shù)據(jù),替換數(shù)據(jù),即可.PHP編程
效果圖:PHP編程
PHP編程
模型層:PHP編程
//顯示列表 public function lists1() { $arr=Yii::$app->db->createCommand("select * from acticle join type on type.t_id=acticle.t_id order by sort desc")->queryall(); return $arr; } //置頂 public function top(){ $arr=$this::find()->select("sort")->asArray()->all(); //print_r($arr);die; $rows=array(); foreach($arr as $key=>$v) { $rows[]=$v['sort']; } $max=array_search(max($rows),$rows); return intval($rows[$max]+1); } //修改數(shù)據(jù) 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; } }
控制器:PHP編程
//文章置頂 public function actionTopq(){ $acticle_id=$_GET['id']; //echo $acticle_id;die; //獲取最大sort $model=new Acticle(); $sort=$model->top(); //修改數(shù)據(jù) $row=$model->update1($sort,$acticle_id); //echo $row;die; if($row==1){ //替換數(shù)據(jù),置頂 $res1=$model->lists1(); $art=new Articles(); $res6=$art->get_right($res1,5); return $res6; }else{ return false; } }
視圖層:PHP編程
<!-- 文章列表 --> <div class="r_230_b ma_b8" style="float:right;right:0;line"> <div class="news_t" ><h2><font color="#d52c99">最新動(dòng)態(tài)</font></h2></div> <?php echo $res6;?> </div> <script> function topq(ts){ $.get("index.php?r=index/topq",{id:ts},function(msg){ $('#sort').html(msg); //alert(msg); }) } </script>
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》PHP編程
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助.PHP編程
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/3005.html