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 回答
0

假设您使用 html 表格来显示它,我只会使用http://datatables.net/,它会很容易地为您提供很多排序选项......

否则 usort():

function cip($a, $b)
{
    if ($a['cip'] == $b['cip']) {
        return 0;
    }
    return ($a['cip'] < $b['cip']) ? -1 : 1;
}

$data = array(
   [0] => array('cip' => 1)
   [1] => array('cip' => 5)
   [2] => array('cip' => 2)
);

usort($data, "cip");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}

假设您的数组看起来像这样:

array(
   [0] => array('cip' => 1)
   [1] => array('cip' => 5)
   [2] => array('cip' => 2)
)

它基本上循环直到一切都被排序,你在回调函数中所做的就是告诉它要比较哪些值......

编辑:

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

好的所以现在你有一个数组中的所有总 cips 和 user_id 对吗?

在你调用的 for 循环之外usort($total_cips, "cip");

然后你有一个排序数组,如果你想改变排序方向,只需将 cip 函数中的greater thana更改为less than

于 2013-08-14T13:52:56.137 回答
-1

这里:

 $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."";

你失踪closing quote$jid。它应该是:

 $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."'";

这可能是您的问题的原因之一。

于 2013-08-14T13:36:11.777 回答