Yii實(shí)現(xiàn)文章列表置頂功能示例
本文實(shí)例講述了Yii實(shí)現(xiàn)文章列表置頂功能的方法。分享給大家供大家參考,具體如下:
我的理解:首先點(diǎn)擊獲取當(dāng)前ID,model層查詢所有sort字段,遍歷數(shù)據(jù),得到最大值,修改數(shù)據(jù),替換數(shù)據(jù),即可。
效果圖:

模型層:
//顯示列表
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;
}
}
控制器:
//文章置頂
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;
}
}
視圖層:
<!-- 文章列表 -->
<div class="r_230_b ma_b8" style="float:right;right:0;line">
<div class="news_t" ><h2><font color="#d52c99">最新動態(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è)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。
- Yii實(shí)現(xiàn)單用戶博客系統(tǒng)文章詳情頁插入評論表單的方法
- Yii框架結(jié)合sphinx,Ajax實(shí)現(xiàn)搜索分頁功能示例
- Yii2實(shí)現(xiàn)讓關(guān)聯(lián)字段支持搜索功能的方法
- Yii2使用dropdownlist實(shí)現(xiàn)地區(qū)三級聯(lián)動功能的方法
- yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能
- yii2.0使用Plupload實(shí)現(xiàn)帶縮放功能的多圖上傳
- 詳細(xì)解讀PHP的Yii框架中登陸功能的實(shí)現(xiàn)
- Yii框架form表單用法實(shí)例
- PHP 基于Yii框架中使用smarty模板的方法詳解
相關(guān)文章
ThinkPHP中__initialize()和類的構(gòu)造函數(shù)__construct()用法分析
這篇文章主要介紹了ThinkPHP中__initialize()和類的構(gòu)造函數(shù)__construct()用法,以實(shí)例形式分析了ThinkPHP中類的初始化時構(gòu)造子類的方法,是采用ThinkPHP進(jìn)行面向?qū)ο蟪绦蛟O(shè)計中比較重要的概念,需要的朋友可以參考下2014-11-11
PHP指定截取字符串中的中英文或數(shù)字字符的實(shí)例分享
這篇文章主要介紹了PHP指定截取字符串中的中英文或數(shù)字字符的實(shí)例,還附帶介紹了過濾字符串中空格的方法,需要的朋友可以參考下2016-03-03
純php打造的tab選項(xiàng)卡效果代碼(不用js)
用php實(shí)現(xiàn)的tab選項(xiàng)卡效果,根據(jù)get判斷,獲取get生成css與對應(yīng)的內(nèi)容,當(dāng)然效率肯定沒有靜態(tài)的好,這里給出的思路與方法。2010-12-12
PHP使用fopen與file_get_contents讀取文件實(shí)例分享
這篇文章主要介紹了PHP使用fopen與file_get_contents讀取文件實(shí)例分享的相關(guān)資料,需要的朋友可以參考下2016-03-03

