Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:35:08+00:00 2026-06-09T13:35:08+00:00

I need that first displays the online users lable, and after that to display

  • 0

I need that first displays the online users lable, and after that to display all the online users, and after that ends the first <optgroup>

the same for Offline and Idle too.

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

i tried to add <optgroup> before <option class='gray_status' for example but then it will be like this

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

and i want like this:

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


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

......

query:

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

Thanks all for the help in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:35:09+00:00Added an answer on June 9, 2026 at 1:35 pm

    If the array is sorted by online status, you can do something simple like this:

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

    If the array isn’t sorted by online status, you can use usort() to sort it prior to running the above code.

    EDIT: Here is usort code that will sort the array in the order you want 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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all I need to say that I'm new to jQuery. When I
i need to put images into a json string.. first is that possible? i
Normally, we get first value that way: $(#color option:first).val() But I need something like
I need a regular expression that will match the first letter of a song
I need a jquery script section that checks to see if the first digit
I need to implement a custom handler for MVC that gives me the first
So basically I need to make feature that if you select something from first
i need to work with setTimeout function but that function does not work. First
I have a menu that displays the first level of child pages of a
I have created a app that displays all the content from res/raw folder. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.