码迷,mamicode.com
首页 > 编程语言 > 详细

php排序算法

时间:2015-01-20 17:35:02      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

功能描述:

  实现对数据点击[向上][向下]按钮,进行排序功能

数据:

  数据id:id

  排序字段:rank_id

if($rankNum>0){
        //echo $id;             //brand_rank
        //echo $rankNum;   //移动到的位置
        //倒序查找前$rankNum个元素
        $rankNum++;
        $query = "SELECT id, brand_rank FROM `#@__archives` WHERE `brand_rank`<={$id} AND `typeid`=28 AND `arcrank`>-1 ORDER BY `brand_rank` DESC limit {$rankNum}";
    }else if($rankNum < 0){
        //echo $id;
        //echo $rankNum;
        //倒序查找后abs($rankNum)个元素
        $rankNum = abs($rankNum);
        $rankNum++;
        $query = "SELECT id, brand_rank FROM `#@__archives` WHERE `brand_rank`>={$id} AND `typeid`=28 AND `arcrank`>-1 ORDER BY `brand_rank` ASC limit {$rankNum};";
    }
    $dsql->SetQuery($query);
    $dsql->Execute();
    while ($row = $dsql->GetArray()){
        $array_id[]=$row["id"];
        $rank_id[] = $row["brand_rank"];
    }
    $count_num = count($array_id);
    $array[‘data‘] = array();
    $j = 0;
    for ($i=1; $i<$count_num; $i++,$j++){
        $query = "UPDATE `#@__archives` SET `brand_rank`={$rank_id[$j]} WHERE `id`={$array_id[$i]}";
        if(!$dsql->ExecuteNoneQuery($query)){
            $array[‘msg‘] = "调整失败,请刷新重试!";
            $array[‘status‘] = 2;
            echo json_encode($array);
            exit();
        }
    }
    $query = "UPDATE `#@__archives` SET `brand_rank`={$rank_id[$j]} WHERE `id`={$array_id[0]}";
    if(!$dsql->ExecuteNoneQuery($query)){
        $array[‘msg‘] = "调整失败,请刷新重试!";
        $array[‘status‘] = 1;
        echo json_encode($array);
        exit();
    }
    $array[‘msg‘] = "调整成功";
    $array[‘status‘] = 0;
    echo json_encode($array);

技术分享

技术分享

 

php排序算法

标签:

原文地址:http://www.cnblogs.com/xiashuo-he/p/4236189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!