首页 > 代码库 > php排序算法

php排序算法

功能描述:

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

数据:

  数据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排序算法