self sorting list of items based on most popular (visited the most).

Usually applied on the FAQ (Frequently Asked Questions) section of some sites.

Or how Reddit’s homepage articles ‘bubble’ up based on the number of votes or visits from users, getting re-sorted all the time. That’s right, you’re building something similarly cool!

TL;DR – HERE it is, in action 🙂


  • the ‘main’ view list page dynamically sorts the list, displaying the item with the highest number of views on top, the next one under, and so on
  • each view item page increments a counter of ‘views’


It can be done in any programming language, so I’ll do this exercise in a few of them, with different persistence layers (aka databases of some sort, either flat files on the same filesystem, either simple local SQLite, or maybe a self contained MongoDB installation)

Let’s start with the simplest of them all, PHP + MySQL, since they’re local to any LEMP host anyway. Not covering how to install MySQL and PHP on a server, that’s easy enough to google for anybody.


So let’s get to it. The 2 web pages that comprise our webapp are:
  1. View the ‘self’ sorting main page
  2. View item page

Alright, first, let’s describe the database table where all our data lives:

We’re mostly interested in the views field, which, is obviously a numeric counter. It’s just where we keep the number of views each item will get. It’s also the number that gets incremented by the act itself of actually viewing an item.



We’ll begin with the second one here, the view item page.

Let’s call this vip.php. Now, at the most basic level, this will be a generic page that views some content stored in the database. It will take a URL parameter (that’s what makes it generic), grabs the content from the database, and then displays it however you’d like.

It also has to increment a counter of some sort, to keep track of how many views, or interactions have there been with this entity.




Second, we create a php file named in which we put the database connection details, such as the name of the database, optionally – the server address if it’s not on the same machine, the table name, the user, and the password, like so:

Third, we create the vip.php file as follows (don’t worry, it’s super easy to follow what it does, it’s almost plain English, also, there are comments):

If it all went well, it should look like this:


Now we continue with what should have been the beginning, which is index.php

Which is the main page that shows us the reverse sorted list of items from the database:



Now, if all this went well, you should see something like so, perhaps sort of default font and colors, since I haven’t included any styling stuff in here, but similar to this below:

See? Easy peasy.



As such, let’s have a quick recap

You should now have 3 files:

  • (has the DB connection and table details for the other two scripts)
  • index.php (the main page that shows you all the items with the number of views for each item)
  • vip.php (view item page, shows you the content of an item, unlike the index page which only shows you the title)



The challenge I extend is to do this same stuff in another programming language. I’ll do this for myself in vanilla javascript, go, python, ruby, and even bash shell script. Just ’cause 😛


If there are any questions, drop a comment below and I’ll get right back to you.

Write a Comment