MySQL query to display 10 of user's friends sorted by most recently corresponded with friends

I created a small chat box on my website that shows 10 thumbnails for a quick way to open a chat window to communicate with the user's friends. The user may have 1000 friends or more, so the 10 thumbnails should be of most recently communicated with friends. Currently I'm just using a simple query displaying 10 of the user's friends sorted by user's friend's last_activity, which is a timestamp. This can become a problem as the site grows and the user has many friends online, the thumbnails will rearrange every few seconds.


$sel = "SELECT friends.user_id, friends.friend_id, [login to view URL], users.user_id, [login to view URL], users.last_activity FROM friends, users WHERE friends.user_id = ".$_SESSION['user_id']." AND users.user_id = friends.friend_id AND [login to view URL] = 2 ORDER BY last_activity DESC LIMIT 12";

$recent_friends = mysqli_query($link,$sel);

while($my_friends = mysqli_fetch_array($recent_friends))


$online_status_msg = "OFFLINE";

$online_status_class = "disconnected";

$onlineStyle="background-color: #ff4f60;";

if(time() - $my_friends['last_activity'] <120) {

$online_status_class = "online";

$onlineStyle="background-color: #32e4cd;";


elseif(time() - $my_friends['last_activity'] <300) {

$online_status_class = "away";

$onlineStyle="background-color: #ffd52f;";


$checkMsgs = mysqli_fetch_array(mysqli_query($link,"SELECT id FROM inbox WHERE user_id = ".$_SESSION['user_id']." AND `author` = ".$my_friends['user_id']." AND `read` = 0 LIMIT 1"));





<li class="inline-items js-chat-open" data-id="<?=$my_friends['user_id']?>">

<div class="author-thumb">

<img alt="author" src="<?=justGetThumbSource($my_friends['user_id'])?>" class="avatar <?=$newMsg?>" style="max-width:34px;">

<span class="icon-status <?=$online_status_class?>" style="<?=$onlineStyle?>"></span>



<? } ?>

The `outbox`

Skills: PHP, MySQL, Software Architecture

See more: edit csv mysql query, asp mysql query, 2013 lost connection mysql query, mysql query report, php mysql query table, mysql query record bash, mysql query csv file, write mysql query, display mysql query ajax menu, display mysql query php table easy, mysql query display results csv file, display mysql query joomla website, display results mysql query, send email display dear user mysql php, iphone app display mysql query, mysql query display php, page display name user mysql, smarty display mysql query, Featured Snippet Writing for Health and Nutrition- Summary of an answer to a user\ s query- 100 questions

About the Employer:
( 1 review ) Bethel, United States

Project ID: #21183599

Awarded to:


Hello hiring member i hope you are doing well i am bhavika i have 4+years experience in php , laravel ,ajax , jquery , javascript , html ,css , bootstrap i have done many things with php , mysql and i can do it and i c More

$25 USD in 1 day
(138 Reviews)

35 freelancers are bidding on average $31 for this job


This is Pavel who is on discussing with you via chatting. Please drop me a message for further discussion. Thanks.

$20 USD in 1 day
(90 Reviews)

I have experience of Website in PHP, Laravel, CodeIgnitor, Wordpress, Opencart, Woocommerce, Photoshop, Adobe Illustrator . We also have experience of making Android,Window and iphone application with ionic & phonegap. More

$20 USD in 1 day
(128 Reviews)

Dear employer! Thanks your your job posting. I read your project and I am interested in this field. As you can see my profile, I have rich experiences in PHP and MySQL. If you hire me, I will do my best and you will se More

$30 USD in 1 day
(33 Reviews)

Hello, I can help you with this just contact me and let's start the work immediately :)

$20 USD in 7 days
(95 Reviews)

CAN COMPLETE WITHIN 1 HOUR. I am a PHP and MySQL expert and have built LOTS of similar systems to yours. I have 5+ years experience with PHP and MySQL. Message me NOW and I can start now.

$50 USD in 1 day
(57 Reviews)

Hello, Nice to meet you. I have over 5 years of Exp around the web. I have checked your project descriptions and think I can help you. If you want to hire me, we can make a good result quickly i think. Please feel fre More

$30 USD in 1 day
(24 Reviews)

Hi, I have read carefully to your proposal and I can help you. As a best DBA, I have rich experience with DB migration, System Speed Tuning, Database Optimization, Big-Data Manipulation and so on. I would like discu More

$50 USD in 1 day
(13 Reviews)

Hi, Got your project requirements here. Thanks for your sharing it. I have been doing back-end and front-end developer with more than 8 years My main skill: PHP(Laravel, Codegniter, Yii, Wordpress), NodeJs, JSP, JQu More

$20 USD in 7 days
(34 Reviews)

Hi, sir. I have read this project very carefully. I am a senior web developer. I have developed a lot of web and mobile apps for 6 years. I have rich experience with Graphic Design, PHP, MYSQL, HTML, CSS, JAVASCRIPT, j More

$20 USD in 7 days
(53 Reviews)

i can solve this right now,i can solve this right now,i can solve this right now,i can solve this right now,i can solve this right now,i can solve this right now

$25 USD in 1 day
(80 Reviews)

Hello :) Thanks for your job posting. I saw your description carefully. I am a Senior web Developer who have 8+ years of experiences. My skills are Html5,CSS,SCSS, Bootstrap,Javascript,Jquery,MySql,PHP especially QUER More

$50 USD in 1 day
(27 Reviews)

Hi, I have just read your requirements carefully. As professional web developer has over 8 years of experience in development. I have rich experience with Laravel, CodeIgniter, Wordpress and Angular.js,React.js,Vue.js More

$30 USD in 7 days
(7 Reviews)

HI, there. I am interested in your task. I have high PHP and Mysql skill and can work anytime. Please contact me and discuss, Thanks.

$20 USD in 1 day
(26 Reviews)

Hi. Here is a skillful full stack web developer. Thank you for your posting. I have 4+ years of experience in projects like yours. Once you choose me, I'll deliver the perfect result Thanks in advance.

$35 USD in 1 day
(19 Reviews)

Dear, client. As you can see my review I'm expert at Javascript and ReactJS and Angular and PHP... Of course Laravel and Codeignitor is perfect. The person who experts at Javascript can clearly finish all web project. More

$50 USD in 1 day
(29 Reviews)

Hi, I'm very glad to bid your project. I've read your job post carefully. This job really attracts me and also your required skills are completely matched with my major skills. I have good knowledge and rich experienc More

$50 USD in 1 day
(16 Reviews)

Hi, dear! I am glad to bid your project. I have many experiences in website design and development. My rich development experience will give you amazing result for your project. I am serious bidder, so I only bid on pr More

$20 USD in 1 day
(20 Reviews)

Hello How are you? Thank you sharing for your post project I am very interesting for your post project. If you want to same other friend information show on your website, i can help you. I think you have got already fo More

$20 USD in 7 days
(10 Reviews)

⭐ Dear Client. ⭐ I have just read your requirements carefully. I am very interested in your project. Although I am new in freelancer, I have more than 5 years of experiences in Database Administration. I'm an expert in More

$30 USD in 2 days
(24 Reviews)

Hello, Client. Nice to meet you. I saw your job description carefully and understood what you want I am so interested in your project and want to work with you, I am senior web developer with rich experience of SQL q More

$17 USD in 1 day
(23 Reviews)