如何在foreach PHP中使用<optgroup>?</optgroup>

时间:2012-08-10 01:20:28

标签: php html

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

离线和闲置也一样。

CODE:

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']));

先谢谢大家的帮助!

2 个答案:

答案 0 :(得分: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代码,它将按您想要的顺序对数组进行排序example

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;
    }
}

答案 1 :(得分: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>"

}