0

最高から最低の cip と最低から最高の cip を並べ替えたいのですが、cip 値はデータベースに保存されず、クエリが起動されるとすぐに計算されます。並べ替えを選択するための HTML コードは次のとおりです。

  <select name="cip">
                    <option>Select CIP percentage</option>
                    <option value="1">Highest To Lowest</option>
                    <option value="2">Lowest To Highest</option>
            </select>

そして、これが私が起動したクエリです!

    $select_applicant = "SELECT j.*, u.id, u.gender
                        FROM job_apply j
                        LEFT JOIN users u
                        ON u.id = j.user_id
                        WHERE j.job_id = '".$jid."'";

    $result_applicant = mysqli_query($con, $select_applicant);
    while($row_applicant = mysqli_fetch_assoc($result_applicant))
    {
        $user_id = $row_applicant["user_id"];
        $user_info = get_user_profile_info($user_id);
        $ratings_dist = get_skill_ratings_stats($user_id);
                $got_it_total_rating = 0;
                foreach($ratings_dist as $category=>$rating)
                {
                    $got_it_total_rating = $got_it_total_rating + $rating['score'];
                }
                $got_it_total_category = count($ratings_dist);
                $total_cip = ceil($got_it_total_rating / $got_it_total_category);

                echo $total_cip;
    }

PHPの関数「get_skill_ratings_stats」は次のとおりです。

function get_skill_ratings_stats($user_id){
    global $con;
    $items = array();
    if($user_id>0)
    {
        $sql = "SELECT 
            s.category_id category_id, 
            c.name as category, 
            COALESCE(r.score_quality, 0.0) quality, 
            COALESCE(r.score_timing, 0.0) timing, 
            COALESCE(r.score_budget, 0.0) budget, 
            COALESCE(r.score_resp, 0.0) resp, 
            COALESCE(r.score_pro, 0.0) pro
        FROM `user_skills` s 
        LEFT JOIN `skill_categories` c
        ON c.category_id=s.category_id
        LEFT JOIN `skill_ratings` r
        ON r.skill_id=s.skill_id 
        WHERE s.user_id = '".(int)$user_id."'
        AND s.status = 'active'
        ORDER BY category ASC";
        $prev_cat   = '';
        $result     = mysqli_query($con, $sql);
        // die(mysqli_error($con));
        while($row = mysqli_fetch_assoc($result))
        {   
            // print_r($row);
            if(!$row['category']) continue;
            if($row['category']!=$prev_cat)
            {
                if(isset($items[$prev_cat]['score']) && $items[$prev_cat]['score']>0 && count($items[$prev_cat])>1)
                {
                    $items[$prev_cat]['score'] = floor(($items[$prev_cat]['score']*20)/(count($items[$prev_cat])-1));
                }
                $prev_cat = $row['category'];
            }
            if(!isset($items[$prev_cat]['score'])) $items[$prev_cat]['score'] = 0;
            $items[$prev_cat][]             = $row;
            $items[$prev_cat]['score']  += ($row['quality']+$row['timing']+$row['budget']+$row['resp']+$row['pro'])/5;
        }
    }
    // print_r($items);
    // die();
    if(isset($items[$prev_cat]['score']) && $items[$prev_cat]['score']>0 && count($items[$prev_cat])>1)
    {
        $items[$prev_cat]['score'] = floor(($items[$prev_cat]['score']*20)/(count($items[$prev_cat])-1));
    }
    $tmp = array();
    foreach($items as $cat=>$item) {
        $tmp[$item['score'].'-'.$cat] = $item;
    }
    krsort($tmp);
    $items = array();
    foreach($tmp as $k=>$v) {
        $k = preg_replace('#^\d+-#is', '', $k);
        $items[$k] = $v;
    }
    // print_r($tmp);
    // print_r($items);
    // die();
    return $items;
}

私を助けてください!これには苦労しました!

4

3 に答える 3

0

We0が言ったように、

 $total_cips[] = array('user_id' => $user_id, 'cip' => ceil($got_it_total_rating / $got_it_total_category));

しかし、私はマルチソート機能を使用しました:

            function multi_sort($array, $akey)
            { 
              function compare($a, $b)
              {
                 global $key;
                 return strcmp($a[$key], $b[$key]);
              }
              usort($array, "compare");
              return $array;
            }

そして、次のように関数を呼び出します。

$total_cips = multi_sort($total_cips, $key = 'cip');

最後に、これはうまくいきました!

とにかくWe0Ankit に感謝

于 2013-10-02T07:08:04.433 に答える