Yii Implementation article List top feature example

Source: Internet
Author: User
Tags yii
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.

  • Related Article

    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.