How to take votes and rank images using PHP and MySQL

sanchit gupta
sanchit gupta used Ask the Experts™
on
I'm trying to build a php ranking system for images. As soon as the user clicks on the image it gets upvoted by 1. Depending on how an image is ranked decides what its place on the leaderboard (rank number) would be. The rank should change depending on the different ratings it receives from users. I want to keep ranking ip based as in like no same user can vote twice.  I have written the frontend code for the same.

Would integrate it if needed.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
We have an article here about voting and ranking.  It probably contains most of the moving parts you would need.
https://www.experts-exchange.com/articles/5256/Simple-Vote-Counting-in-PHP-and-MySQL.html

Author

Commented:
@Ray Doing that with images would be a little different. Could you please write an article for the same?
Most Valuable Expert 2011
Top Expert 2016
Commented:
I think any knowledgeable and experienced developer could help you bridge the gap between the article (clicking a checkbox) and your needs (clicking an image), so I recommend you use E-E Gigs and find a developer who can help you.

To the part about "no same user can vote twice" you will get uneven results if you use the IP address as the gating factor.  What if a user is on AOL, where the IP address changes from request to request?  What if the user is in an office, where everyone communicates through a server with the same IP address?  If you're willing to accept these risks, just mark the IP address column UNIQUE in your table of votes.  When an attempt is made to insert a duplicate value, MySQL will not insert the duplicate and will return errno=1062.  Your application can trap this error and take whatever action you want.  One thought might be to request a payment before entering another vote.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
gigs project cost money
Most Valuable Expert 2011
Top Expert 2016

Commented:
Of course Gigs cost money!  Why?  Because they're worth it.  

You get value from the help of a knowledgeable and experienced developer.  The alternative is for you to take the time and effort to learn everything a developer already knows, and then do the work yourself.  Even in a concentrated learning environment such as a university, you would need months to learn what you're asking me to write an article about.  If I had the "free time" to write the article, I would consider it.  But the truth is that all of the information a developer would need is already in the article about vote counting.

If you're new to PHP and want to learn the language, this article can help.  It will show you good learning resources and help you avoid the many dangerous, outdated and incompetent PHP examples that litter the internet.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
Hi sanchit.  I think the disconnect with Ray is for something like this we probably are not going to write out an entire program.  But we can help you.  I suggest breaking your request to multiple questions where this first question only goes over how this is done.   From there you can work on the individual parts and ask separate questions for each where you show what you have so far or where you need help to get started.

In very simplistic terms, this can work by:

1) Create a database with 2 tables.  One table will have an id, image name and image description.  Let's call this table, 'tblImages'.  Your 2nd table will be a transaction table that tracks each upvote.  This table will have one field called image_id and lets call this table, tblImageUpVotes.  

For now, we are keeping this simple. Complexity can be added later.

2) For this example, let's assume you have a finite number of images that you uploaded to your server and have also added to your tbleImages.

Again, more advanced features will be uploading images and adding that to the db.  But for now, we keep it simple.

3) Create a simple HTML page that displays all the images with an html table at the top that contains the leader board of upvotes.

4) Each time an image is clicked, we want to add the image id (from the database) to our transaction table called tblImageUpVotes.  In the end, this table will be filled with a bunch of id's.

5) To figure out which image has been clicked the most, we create a group by query that counts the id's https://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html

6) We will place use the group by query and sort it by the count in descending order and display that on our leader board.


This is a very basic outline and if this sounds similar to what you want to work with, let's iron out the overview here in this question.  Then you can ask specific questions for each of the steps.
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Commented:
Also, you will see when you do accept answer(s) for this thread, there will be an, "Ask a related question" button.  That will link this question to your next.  Keep doing that for each of the steps and that will give the Experts a way to reference what has already taken place.
Most Valuable Expert 2011
Top Expert 2016

Commented:
abandoned question - hopefully taken up in Gigs

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial