Hello everybody,
i learned the basics of php a while ago, and as a first project, i want to create a website where anybody can rate pictures or upload his own.
The hosting isnt a problem, i want to know if a db is necessary, if its i will have to learn mysql.
note:No login/anybody can upload.
Thanks.

A database is not necessary, but it will probably much easier to store/retrieve your ratings from a database, then for example a file.

Ok thanks

I would go with a database also and for your example you'd probably only need to look up three PHP 'functions' to work with the database: Insert (to create a new row the first time that a rating is left for an image), Select (to get the ratings for a photo from the database to be displayed on the page) and Update (to change the rating when someone rates the photo).

You can create a table called 'ratings'. If every image will have a unique name then you can create a column like 'image_name' and it would be the unique key. Then have a column called 'rating'. You may also want to create a column called 'votes' because then you can code the ratings page to pull the rating total and number of votes..divide the total rating by the number of votes and round the number to the nearest whole number which will give you the average rating to be posted.

Then when someone rates the photo you add the rating number they vote and add it to the total votes and increment the votes total by one. Update that image's database row with the new ratings total and vote total.

One problem with this method is that since you are allowing user uploads you can't guarantee that the image file's name will always be unique. But you can write the uploading script (or modify/use an existing) to rename the file like, say, 1.jpg, 2.jpg and so on. This way you know that each image file's name WILL be unique which the database would need.

You can keep this all located in one script file and create a function like 'cast vote' and give links like:

image_rating.php?action=cast_vote&image=$image_name&rating=1

Now offer a link for however many numbers you want them to be able to vote such as 1 - 5. Just change the rating=# for each link.

Then have the following:

<?php
$action = $_GET['action'];
$image = $_GET['image'];
$rating = $_GET['rating'];

if ($action == "cast_vote") {cast_vote();}

function cast_vote() {
global $image;
global $rating;

#Write the code to increment the ratings total number and increment the number of votes by one then update that image's database row with those numbers.
}

It would be a little more advanced than this but not by much.

This site may help you out with not only the MySQL queries but other aspects of the PHP you'd need to use: http://www.tizag.com/mysqlTutorial/ Then use the MySQL Tutorial menu to the left for examples.

Best of luck.

Thanks, i will first design it on localhost, then i will test it and upload it
thanks

Hi Karlwakim,

This is a good idea, i recommend using http://www.verot.net/php_class_upload.htm for the image uploads. Also you could create a database for the images for commenting and rating purposes.

I would create a database with all the images (id, title, description, rateing). Then maybe another table with all the comments (id, img_id, comment, datetime), linking to the images table id.

When uploading the images, just upload to a folder. And give the image a name like image_<image_id>.jpg. Constraint the file extension. Also try and upload different version of the image, maybe a thumbnail, medium and the original.

But that this looks like an easy project, so good luck. :)

Thanks,
Marais

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.