0

我需要首先显示在线用户标签,然后显示所有在线用户,然后结束第一个<optgroup>

离线和空闲也是如此。

代码:

foreach($alt_user_result as $cur_user) {
    if( $cur_user[user_status]=='online') {
        echo "<option class='green_status' value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]
        </option>";
    }
    elseif( $cur_user[user_status]=='idle') {
        echo "<option class='yellow_status' value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]
        </option>";
    }
    elseif( $cur_user[user_status]=='offline') {
        echo "<option class='gray_status' value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]
        </option>";
    }
}

<optgroup>例如,我之前尝试添加<option class='gray_status',但随后会是这样

<optgroup><option></option><optgroup>
<optgroup><option></option><optgroup>
<optgroup><option></option><optgroup>
<optgroup><option></option><optgroup>
<optgroup><option></option><optgroup>

我想要这样:

    <optgroup lable="online">
    <option></option>
    <option></option>
    <option></option>
    <optgroup>


    <optgroup lable="offline">
    <option></option>
    <option></option>
    <option></option>
    <optgroup>

......

询问:

$alt_user_result = execute_pdo_query("SELECT U.username, U.user_id, T.last_activity_time,
case WHEN TIMESTAMPDIFF( MINUTE, T.last_activity_time ,NOW() ) <= 5 THEN 'online'
 WHEN ( TIMESTAMPDIFF( MINUTE, T.last_activity_time ,NOW() ) > 5 AND
  TIMESTAMPDIFF( MINUTE, T.last_activity_time ,NOW() ) <=30 ) THEN 'idle'
    ELSE 'offline' end as user_status
     FROM user_skills S, users U
      LEFT JOIN user_status T ON U.user_id=T.user_id
         WHERE S.user_id=U.user_id and S.task_type_id=? and S.permission='Yes';",
           array($tablerow['task_type_id']));

提前感谢大家的帮助!

4

2 回答 2

2

如果数组按在线状态排序,您可以执行以下简单操作:

$prevGroup = '';

foreach($alt_user_result as $cur_user) {
    $curGroup = $cur_user['user_status'];

    if ($curGroup != $prevGroup) {
        if ($prevGroup != '') {
            echo '</optgroup>';
        }

        echo '<optgroup label="' . ucfirst($curGroup) . '">';
        $prevGroup = $curGroup;
    }

    if ($cur_user[user_status] == 'online') {
        echo "<option class='green_status' value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]
    </option>";
    } else if ($cur_user[user_status] == 'idle') {
        echo "<option class='yellow_status' value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]
    </option>";
    } else if ($cur_user[user_status] == 'offline') {
        echo "<option class='gray_status' value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]
    </option>";
    }
} // foreach

echo '</optgroup>';

如果数组没有按在线状态排序,您可以usort()在运行上述代码之前对其进行排序。

编辑:这是usort将按照您想要的顺序对数组进行排序的代码示例

function userStatusSort($a, $b) {
    $a = $a['user_status'];
    $b = $b['user_status'];

    if ($a == $b) {
        return 0;
    } else if ($a == 'online') {
        return -1;
    } else if ($a == 'idle' && $b == 'offline') {
        return -1;
    } else {
        return 1;
    }
}
于 2012-08-10T01:27:09.517 回答
0

我首先将用户分类到单独的数组中

$online_users = array();
$offline_users = array();
$idle_users = array();

foreach($alt_user_result as $cur_user) {
    if($cur_user[user_status]=='online'){
        $online_users[] = $cur_user;
    }
 //and so forth...

}

然后回显它们

 echo "<optgroup>"
 foreach($online_users as $cur_user){
    echo "<option class='green_status' value='$cur_user[user_id]'>$cur_user[username] | $cur_user[user_status]
</option>";
echo "</optgroup>"

}
于 2012-08-10T01:30:32.900 回答