Free Domain Names






Post new topic Reply to topic  [ 7 posts ] 
How do I create a 10 star rating system? 
Author Message
Newbie

Joined: Sun Nov 01, 2009 7:44 am
Posts: 21
Post How do I create a 10 star rating system?
Basically just above. Preferred 10 stars, and going into the 100ths (eg.8.76)
but any thing will do.
Cheers


Edit By Paka: Moved this to webprogramming, think it fits better there


Tue Nov 03, 2009 6:14 am
Profile
Jr. Member

Joined: Wed May 06, 2009 7:20 pm
Posts: 251
Post Re: How do I create a 10 star rating system?
this isn't hard to do, but the way it is done depends on your page setup.
The rating needs ofcourse to be connected to a certain game, (I think in your case we are rating games right? ), and ...
well, let me try to give you an example, might be easier to understand then me just rambling on ... :)

This example assumes that you are loading your games though a database by setting a game value in the URL, ( in php get this value by using $_GET['game']
example of an url.. http://www.thegamezone.co.nr/platform/games.php?game=12
and it assumes that tou have a table called: game_ratings
which contains apart from an id Primary key: game_id, number_of_ratings, rating

I havent tested this code so there might be some errors in it, but you should get the idea..
Yes, it can be compressed alot, but for the examples sake I have tried to be as clear as possible.

Hopefully you'll get the idea and create a better one, more suted for your exact needs :)

Code:
<?php
   // see if we have selected a game.
   // example URL: http://www.thegamezone.co.nr/platform/games.php?game=12
   // Now we are playing platform game no. 12
if(isset($_GET['game'])){   
   // Connect to and select your database
   
   $game = mysql_real_escape_string($_GET['game']));
      // Now $game is safe to use in a database query.
      
      // Put code for loading the game here.
      // ***
      
      // See if we just clicked rate
   if(isset($_POST['gamerating'])){
         
      // we did just click rate! so do the actual rating.
      $rating = $_POST['RatedValue'];
      $result = mysql_query("SELECT * FROM game_ratings WHERE game_id = '$game'");
      
      //Check if the current game has been rated before
      if(mysql_num_rows($result)>0){
         while($row = mysql_fetch_array($result)){   
            $num_ratings = $row['number_of_ratings'];
            $current_rating = $row['rating'];
            
            // do the math
            $to_insert = $current_rating * $num_ratings;
            $to_insert = $to_insert + $rating;
            $num_ratings = $num_ratings + 1;
            $to_insert = $to_insert / $num_ratings;
            $to_insert = round($to_insert,2);
            
            // update database
               mysql_query("UPDATE game_ratings SET number_of_ratings = '$num_ratings', rating = $to_insert WHERE game_id = '$game'");
            }
      }else{
         // this game has not been rated, create a row for it.
         mysql_query("INSERT INTO game_ratings (number_of_ratings, rating, game_id VALUES('1','$rating','$game')");
      }
   }else{
      
      // otherwise show the rate form
      // Here goes a simple rate form.
      echo "<form action=\"\" method=\"post\">";
      echo "<select name=\"RatedValue\">";
      for($i=0;$i<11;$i=$i+1){
   
         // Create a dropdownlist with values 1-10
         echo "<option value=\"".$i."\">".$i."</option>";
      }   
      echo "</select>";
      echo "<input type=\"submit\" value=\"Rate\" name=\"gamerating\" />";
      echo "</form>":   
   }
   
   // close the database connection here.   

}else{
   // This is what will be seen of no game is selected
}
?>

_________________
//Paka
>>Forum Moderator<<
polsson.co.cc


Tue Nov 03, 2009 7:51 pm
Profile
Newbie

Joined: Sun Nov 01, 2009 7:44 am
Posts: 21
Post Re: How do I create a 10 star rating system?
I don't understand.
I had planned on simply making a html web page for every game, but this way seems much better. I would need help to understand using php for it.
I did as much as you suggested as I could, here is what I did.


I have made a database called "games".
Added a table called "game_ratings"
And added columns named

game_id
number_of_ratings
rating

Now what should I do?


Wed Nov 04, 2009 5:46 am
Profile
Jr. Member

Joined: Wed May 06, 2009 7:20 pm
Posts: 251
Post Re: How do I create a 10 star rating system?
as much as I would enjoy coding the site for you i realyl don't have the time for it.

I suggest you try googling for "php dynamic sites" or something similar.

You really should as much as possible write your code on your own.
That will make searching for errors and altering the code later much easier as you will know what and where the codes are.

also you should consider keeping all your db info (username, password, db name and so on) in a seperate file that you include (include_once filename.php;)
Basically you should keep all code that is used in many pages in a seperate file that you include. The first time you need to change something, the db password for example, you will be very happy that you only need to change it in that one file instead of in every file it is used!

anyhow...
I don't know if you plan to host the games or just embed them from somewhere else..
but assuming you do not host them yourself.

a simple table containing

game_id auto increase PK
number_of_ratings
rating
embed_code
genre

will get you started,
using a page similar to the one I showed you in the last post you can then extract everything needed to load a game.
basically where it says
// Put code for loading the game here.
// ***
do a mysql_query and select the embed_code and echo it to the page.

Genre can for example be "platform", so that you in the menus can get all games it that genre.

If you build your site this way, it will when it's done be "easy" to manintain. But there will be some intense coding for you until you get all the menus and other stuff done :)

I think you should read through the code i wrote earlier and try to understand what it is doing.
w3school and php.net will help you alot! and ofc google :)

and feel free to ask for more help here!
and perhaps someone else than me can give you more help on this

good luck!

_________________
//Paka
>>Forum Moderator<<
polsson.co.cc


Wed Nov 04, 2009 9:04 am
Profile
Newbie

Joined: Sun Nov 01, 2009 7:44 am
Posts: 21
Post Re: How do I create a 10 star rating system?
So all the codes that I should need will be in those websites? That sounds handy, Ill ask again if I need help, which no doubt I will.
Thanks


Thu Nov 05, 2009 5:04 am
Profile
Newbie

Joined: Sun Nov 01, 2009 7:44 am
Posts: 21
Post Re: How do I create a 10 star rating system?
paka wrote:
also you should consider keeping all your db info (username, password, db name and so on) in a seperate file that you include (include_once filename.php;)
Basically you should keep all code that is used in many pages in a seperate file that you include. The first time you need to change something, the db password for example, you will be very happy that you only need to change it in that one file instead of in every file it is used!

How do I send the information to this file? What I mean is how do I create the file and use the infromation on the other codes?


Fri Nov 06, 2009 7:11 am
Profile
Jr. Member

Joined: Wed May 06, 2009 7:20 pm
Posts: 251
Post Re: How do I create a 10 star rating system?
something like this... only an example ofcourse, you don't need to use these exact folders or filenames :)
create a folder called includes, put all the files you will want to include here.

file: includes/dbinfo.php
Code:
<?php
$db = array(
'hostname' => 'localhost',
'username' => 'your_username',
'password' => 'your_password',
'database' => 'the_database',
);
?>


file: includes/dbcon.php
Code:
<?php
function dbcon() {
if(!include_once('dbinfo.php')) {
die('Error include file...');
}
if (!$con = mysql_connect($db['hostname'],$db['username'],$db['password'])) {
die('Error connecting...');
}
if (!mysql_select_db($db['database'],$con)) {
die('Error selecting...');
}
return $con;
}
?>


now on the ordinary pages whenever you need to connect to a database just use this:
Code:
   
include_once 'includes/dbcon.php';
$link = dbcon();

now you will be connected to the db throughout the page, you wont have to enter the db info again,
for example to select everything from a table called paka_example in database "the_database" (as entered in phpinfo.php) just use this.
Code:
$result = mysql_query("SELECT * FROM paka_example");


notice that i didn't need to specify the connection or database.

you can then at the end of the page close the connection with
Code:
mysql_close($link);

_________________
//Paka
>>Forum Moderator<<
polsson.co.cc


Fri Nov 06, 2009 11:25 am
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Free Web Hosting | Free Web Hosting Forum | Emenace | Free Web Host

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin