| | |
Rateing option using php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
You must do that yourself. Assigning a rating is merely a thing of figuring out how to rate the thing and writing the code to handle it.
www.uncreativelabs.net
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Note sure what you want really. Its not too clear..
If you want to write a rating script in PHP, then what you'll have to do is.
Write a database table to hold the "item" being rated and its rating.
A simple table say item_ratings, will have the columns: id, item_id, item_type, item_name, item_rating.
NOTE: This is a generic table, which allows you to add any item that can be rated.
For example:
If you're adding a rating for a script that has the id of 2 in the db table it is listed, you just add using mysql like such: INSERT INTO item_ratings SET item_id = 2, item_type = 'script', item_name = 'Example PHP script'
Another example:
If you're adding a user to be rated, and the user_id is 4 you can do is as such: INSERT INTO item_ratings SET item_id = 4, item_type = 'user', item_name = 'Joe'
Then you'll have to create a html page (form) to rate the Items in your rating database table.
The form will then send the rating of the user to your php script and your php script will simply add this rating to your item_ratings table.
First you'll have to insert the new rating into the table.
The SQL would look like:
SELECT item_rating from item_ratings WHERE id = [id]; WHERE [id] is the id of the item being rated.
(if it doesnt exist, you can insert a row for the rating... )
If it exists, then update is.
UPDATE item_ratings SET item_rating = [new rating];
When I first thought of this problem, I thought it would be simple to add a new rating. It would be just one database entry for each Item being rated, and then each time there is a new rating, you just get the old average, and sum it with the new rating, divide by 2 and you have the new rating...
but I realized that:
pseudo code: average(a, b, c) != average( average(a, b), c);
So you cannot have just one entry in the database and keep the database normalized. You'd either have to create a new table just to link item_ratings with each rating made by a user.
It could say have the columns, item_ratings_id, rating.
And in item_ratings instead of having a column item_rating, you could make it avg_rating.
That way you can sum up all the ratings, then divide by the number of ratings to get the average.
The other way is to break normalization of your database and just have all past ratings stored in the column item_rating as TEXT.
If you want to write a rating script in PHP, then what you'll have to do is.
Write a database table to hold the "item" being rated and its rating.
A simple table say item_ratings, will have the columns: id, item_id, item_type, item_name, item_rating.
NOTE: This is a generic table, which allows you to add any item that can be rated.
For example:
If you're adding a rating for a script that has the id of 2 in the db table it is listed, you just add using mysql like such: INSERT INTO item_ratings SET item_id = 2, item_type = 'script', item_name = 'Example PHP script'
Another example:
If you're adding a user to be rated, and the user_id is 4 you can do is as such: INSERT INTO item_ratings SET item_id = 4, item_type = 'user', item_name = 'Joe'
Then you'll have to create a html page (form) to rate the Items in your rating database table.
The form will then send the rating of the user to your php script and your php script will simply add this rating to your item_ratings table.
First you'll have to insert the new rating into the table.
The SQL would look like:
SELECT item_rating from item_ratings WHERE id = [id]; WHERE [id] is the id of the item being rated.
(if it doesnt exist, you can insert a row for the rating... )
If it exists, then update is.
UPDATE item_ratings SET item_rating = [new rating];
When I first thought of this problem, I thought it would be simple to add a new rating. It would be just one database entry for each Item being rated, and then each time there is a new rating, you just get the old average, and sum it with the new rating, divide by 2 and you have the new rating...
but I realized that:
pseudo code: average(a, b, c) != average( average(a, b), c);
So you cannot have just one entry in the database and keep the database normalized. You'd either have to create a new table just to link item_ratings with each rating made by a user.
It could say have the columns, item_ratings_id, rating.
And in item_ratings instead of having a column item_rating, you could make it avg_rating.
That way you can sum up all the ratings, then divide by the number of ratings to get the average.
The other way is to break normalization of your database and just have all past ratings stored in the column item_rating as TEXT.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Sep 2006
Posts: 44
Reputation:
Solved Threads: 3
•
•
•
•
When I first thought of this problem, I thought it would be simple to add a new rating. It would be just one database entry for each Item being rated, and then each time there is a new rating, you just get the old average, and sum it with the new rating, divide by 2 and you have the new rating...
but I realized that:
pseudo code: average(a, b, c) != average( average(a, b), c);
So you cannot have just one entry in the database and keep the database normalized. You'd either have to create a new table just to link item_ratings with each rating made by a user.
It could say have the columns, item_ratings_id, rating.
And in item_ratings instead of having a column item_rating, you could make it avg_rating.
That way you can sum up all the ratings, then divide by the number of ratings to get the average.
The other way is to break normalization of your database and just have all past ratings stored in the column item_rating as TEXT.
UPDATE ratings SET sumRatings=sumRatings+{$newRating}, numRatings=numRatings+1 where id={$id}
then your average is just
select sumRatings/numRatings from ratings where id={$id}
•
•
•
•
well, all you need to keep track of then is the sum and the number of ratings so that you when you get a new rating, you'd just do
UPDATE ratings SET sumRatings=sumRatings+{$newRating}, numRatings=numRatings+1 where id={$id}
then your average is just
select sumRatings/numRatings from ratings where id={$id}
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- URL Rewrite in PHP (PHP)
- Hide right click option usnig php (PHP)
- Problems using a php generator (PHP)
- Adding Compiler Options for PHP in Linux (PHP)
- if statement in php (PHP)
Other Threads in the PHP Forum
- Previous Thread: preg_match validation
- Next Thread: admin_password= is not working
| Thread Tools | Search this Thread |
apache api archive array basics beginner binary broken buttons cakephp checkbox class clients cms code codingproblem combobox cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html if...loop image include insert ip javascript joomla limit link list login mail memmory menu mlm multiple mysql number oop parameter password paypal pdf php phpincludeissue problem query radio random recourse recursion recursive remote script search seo server sessions shot smarty source sp space speed sql static syntax system table tutorial up-to-date update upload url validator variable vbulletin video web webdesign youtube






