本篇文章为大家展示了利用Yii怎么实现一个文章列表置顶功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
模型层:
//显示列表
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);
}
//修改数据
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();
//修改数据
$row=$model->update1($sort,$acticle_id);
//echo $row;die;
if($row==1){
//替换数据,置顶
$res1=$model->lists1();
$art=new Articles();
$res6=$art->get_right($res1,5);
return $res6;
}else{
return false;
}
}
视图层:
<!-- 文章列表 -->
<div class="r_230_b ma_b8" >
<div class="news_t" ><h3><font color="#d52c99">最新动态</font></h3></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>
上述内容就是利用Yii怎么实现一个文章列表置顶功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。