Hi friendds

I need to reating the messages using php code. I need some suggestion.

For example some sites scripts are rated in ***** Tha same action im-plemet my applicaiton.

How to add ? please send me any ideas

Thanks
vssp

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.

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.

Thansk ether

I will complete the task and let u know

vssp


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.

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}

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}

Thanks.. i knew i was missing something there...

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.