This is the SQL query to use.
If you don't know how to add this using phpmyadmin, refer to Leo's tutorial.
| CODE |
CREATE TABLE `RateTable` ( `ID` INT NOT NULL AUTO_INCREMENT , `rateID` INT NOT NULL , `IP` TEXT NOT NULL , `vote` TINYINT NOT NULL , `date` DATE NOT NULL , INDEX ( `ID` ) ); |
| CODE |
<?php $dbHost =""; #Your db host $dbUser =""; #username $dbPass =""; #Pass $dbname =""; #Name of the database. ?> |
| CODE |
<?php function dbinsans($text) { $text = strip_tags ($text, ""); $text = str_replace(chr(10),"",$text); $text = str_replace(chr(13), "<br>", $text); $text = str_replace("\"","*",$text); $text = str_replace("'","*",$text); $text = addslashes($text); return($text); } ?> |
| CODE |
<?php $ip = $_SERVER["REMOTE_ADDR"]; #logs user IP (used to check if they voted) if (isset($_POST['submit'])) { #if the user has clicked the submit button $vote = dbinsans($_POST['select']); #how the user voted, 0 - 10 $rateID = dbinsans($_POST['rateid']); #which poll download they voted on! Allows multiple polls #the next few pieces of code check the user IP against database at ID $r=0; #We're going to use the variable r as a switch to determine if they're in the database. $db = mysql_connect("$dbHost","$dbUser","$dbPass"); #connecting to the database area mysql_select_db($dbname,$db); #selecting our database $requete = "SELECT IP FROM RateTable WHERE rateID='$rateID'"; #A query to pull IPs from the RateTable at the ID $result = mysql_query ($requete,$db); #Should return ips of voters while ($article = mysql_fetch_object($result)) { #scrolls through the ips if ($article->IP == $ip) { $r=1; #if they get here they're already in the system. Set r (our switch variable) to 1 } } if ($r==0) { #if r is 0 they were not found in the previous query $sql = "INSERT INTO RateTable (rateID, IP, vote, date) VALUES ('$rateID', '$ip', '$vote', curdate())"; #Creates the insert query string. mysql_query($sql, $db); #Queries the database and adds the user. } } ?> |
| CODE |
<?php $currentID= '1'; #Ok here is where it counts, the following is the code to display the select box. #Let me tell you why currentID is important, let's say you grabbed this from #A query of your file? Like a page that's listing it and describing it in detail #You could set $currentID = to whatever variable you have for the download's unique ID #AKA two tables and you'd be using the unique ID of the downloaded item and the #ID here to connect them. It's up to you... You could just manually change this though. #Just change 1 to whatever # of your downloads you want. #check IP against database at ID code again $r=0; #r as a switch variable (I don't know if that's a recognized term, but I'm using it as a #switch so I figured I'd call it that. Not to be confused with a switch statement. $db = mysql_connect("$dbHost","$dbUser","$dbPass"); mysql_select_db($dbname,$db); $requete = "SELECT IP FROM RateTable WHERE rateID='$currentID'"; #A query to pull IPs from the RateTable at the ID $result = mysql_query ($requete,$db); #Should return ips of voters while ($article = mysql_fetch_object($result)) { if ($article->IP == $ip) { $r=1; #if you want this code in detail, look above it's the same as I used up there. } } if ($r==0) #if there is no ip of our user in the database { if (!isset($_POST['submit'])) { #if submit has not been pressed, notice the ! #the following is the form... if you know html most of it #should be recognizable... the $_SERVER['SCRIPT_NAME'] is a global #variable and returns whatever you name this file. echo '<form method="post" action="'.$_SERVER['SCRIPT_NAME'].'">'; echo '<input name="rateid" value="'.$currentID.'" type="hidden" />'; echo ' <select name="select">'; echo ' <option value="1">1 - It\'s really bad.</option>'; echo ' <option value="2">2 - It\'s bad.</option>'; echo ' <option value="3">3 - It\'s alright.</option>'; echo ' <option value="4">4 - It\'s good.</option>'; echo ' <option value="5">5 - It\'s really good.</option>'; echo ' </select><br />'; echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />"; echo '</form><br />'; } } else { #already voted area if their ip is in the db then they see this. $one = 0; $two = 0; $three = 0; $four = 0; $five = 0; #Setting all these variables to 0... $db = mysql_connect("$dbHost","$dbUser","$dbPass"); mysql_select_db($dbname,$db); $requete = "SELECT vote FROM RateTable WHERE rateID='$currentID'"; #A query to pull all the votes, but no other part, from the RateTable at the ID $result = mysql_query ($requete,$db); while ($article = mysql_fetch_object($result)) { switch ($article->vote) #the wonderful switch statement the vote should have 0-10 { case 1: #if it equals 1 $one++; break; case 2: #and so on $two++; break; case 3: $three++; break; case 4: $four++; break; case 5: $five++; break; } } $total = $one + $two + $three + $four + $five; #A little algebra. #The round function works like this round($integer,amount after decimal to round to)... #If you know html and css this shouldn't be too bad. #The image I have growing by %... It will fill the box it's in. #you can edit at your own will :woot: have fun echo 'Total Votes '.$total.'<br />'; if ($total==0) # if the total happens to be zero $total=1; #this way it still shows the accurate votes but also will not cause a div by zero error. echo 'It\'s really bad. - '.round((($one/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($one/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s not up to par. - '.round((($two/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($two/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s alright. - '.round((($three/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($three/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s good. - '.round((($four/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($four/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s really good. - '.round((($five/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($five/$total)*100),0)).'%; height:15px;" alt="" /><br />'; } ?> |
| CODE |
| echo ' <select name="select">'; echo ' <option value="1">1 - It\'s really bad.</option>'; echo ' <option value="2">2 - It\'s bad.</option>'; echo ' <option value="3">3 - It\'s alright.</option>'; echo ' <option value="4">4 - It\'s good.</option>'; echo ' <option value="5">5 - It\'s really good.</option>'; echo ' </select><br />'; |
| CODE |
| echo '<input type="radio" name="select" value="1">1 - It\'s really bad.<br />'; echo '<input type="radio" name="select" value="2">2 - It\'s bad.<br />'; echo '<input type="radio" name="select" value="3">3 - It\'s alright.<br />'; echo '<input type="radio" name="select" value="4">4 - It\'s good.<br />'; echo '<input type="radio" name="select" value="5">5 - It\'s really good.<br />'; |
| CODE |
| echo '<form method="post" action="'.$_SERVER['SCRIPT_NAME'].'" />'; echo '<input name="rateid" value="'.$currentID.'" type="hidden" />'; echo ' <select name="select">'; echo ' <option value="1">1 - It\'s really bad.</option>'; echo ' <option value="2">2 - It\'s bad.</option>'; echo ' <option value="3">3 - It\'s alright.</option>'; echo ' <option value="4">4 - It\'s good.</option>'; echo ' <option value="5">5 - It\'s really good.</option>'; echo ' </select><br />'; echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />"; echo '</form><br />'; |
| CODE |
| $one = 0; $two = 0; $three = 0; $four = 0; $five = 0; #Setting all these variables to 0... |
| CODE |
| $six = 0; |
| CODE |
| switch ($article->vote) #the wonderful switch statement the vote should have 0-10 { case 1: #if it equals 1 $one++; break; case 2: #and so on $two++; break; case 3: $three++; break; case 4: $four++; break; case 5: $five++; break; } |
| CODE |
| $six++; break; |
| CODE |
$total = $one + $two + $three + $four + $five; |
| CODE |
$total = $one + $two + $three + $four + $five+$six; |
| CODE |
echo 'It\'s really bad. - '.round((($one/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($one/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s not up to par. - '.round((($two/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($two/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s alright. - '.round((($three/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($three/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s good. - '.round((($four/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($four/$total)*100),0)).'%; height:15px;" alt="" /><br />'; echo 'It\'s really good. - '.round((($five/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($five/$total)*100),0)).'%; height:15px;" alt="" /><br />'; |
| CODE |
| echo 'It\'s perfect. - '.round((($six/$total)*100),2).'%<br /><img src="rating.gif" style="width:'.(round((($six/$total)*100),0)).'%; height:15px;" alt="" /><br />'; |
| QUOTE (Josh @ Mar 12 2004, 10:55 PM) |
| awesome tutorial timo! |
| QUOTE |
| I suggest you put this in an external file and include ''; it, but it's up to you. It'll work fine if you put it in the code, I find it annoying to retype though. Or if you decide to switch servers, you're going to have a lot of fun changing them all. |
| CODE |
<?php $dbHost =; #Your db host $dbUser =; #username $dbPass =; #Pass $dbname =; #Name of the database. ?> |
| CODE |
| <?php $dbHost =; #Your db host $dbUser =; #username $dbPass =; #Pass $dbname =; #Name of the database. ?> |
| CODE |
| <?php include 'serverinfo.php';?> |
| CODE |
| echo ' <select name="select">'; echo ' <option value="0">0 - It sucks, alot.</option>'; echo ' <option value="1">1 - It\'s really really bad.</option>'; echo ' <option value="2">2 - It\'s really bad.</option>'; echo ' <option value="3">3 - It\'s bad.</option>'; echo ' <option value="4">4 - It\'s semi-alright.</option>'; echo ' <option value="5">5 - It\'s alright.</option>'; echo ' <option value="6">6 - It\'s good.</option>'; echo ' <option value="7">7 - It\'s really good.</option>'; echo ' <option value="8">8 - It\'s really really good.</option>'; echo ' <option value="9">9 - It\'s excellent.</option>'; echo ' <option value="10">10 - It\'s perfect.</option>'; echo ' </select><br />'; |
| CODE |
echo '<input type="radio" name="select" value="0">0 - It sucks, alot.<br />'; echo '<input type="radio" name="select" value="1">1 - It\'s really really bad.<br />'; echo '<input type="radio" name="select" value="2">2 - It\'s really bad.<br />'; echo '<input type="radio" name="select" value="3">3 - It\'s bad.<br />'; echo '<input type="radio" name="select" value="4">4 - It\'s semi-alright.<br />'; echo '<input type="radio" name="select" value="5">5 - It\'s alright.<br />'; echo '<input type="radio" name="select" value="6">6 - It\'s good.<br />'; echo '<input type="radio" name="select" value="7">7 - It\'s really good.<br />'; echo '<input type="radio" name="select" value="8">8 - It\'s really really good.<br />'; echo '<input type="radio" name="select" value="9">9 - It\'s excellent.<br />'; echo '<input type="radio" name="select" value="10">10 - It\'s perfect.<br />'; |
| QUOTE (joe2kiss @ Mar 13 2004, 02:00 PM) |
| made some changes, still got errors though could you take a look at it for me please |
| QUOTE |
| Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 72 Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 72 Zero - 0% Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 73 Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 73 One - 0% Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 74 Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 74 Two - 0% Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 75 Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 75 Three - 0% Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 76 Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 76 Four - 0% Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 77 Warning: Division by zero in c:\program files\easyphp1-7\www\rateit\ratings.php on line 77 |
| CODE |
echo '<tr><td colspan="2">Data Table for Download with the ID '.$rateID.' ... The total amount of votes - '.$total.'</td></tr>'; |
| CODE |
echo '<tr><td colspan="2">Data Table for Download with the ID '.$rateID.' ... The total amount of votes - '.$total.'</td></tr>'; if ($total==0) # if the total happens to be zero $total=1; #this way it still shows the accurate votes but also will not cause a div by zero error. |
| QUOTE (joe2kiss @ Mar 13 2004, 02:17 PM) |
| you are right, it was because there were no values in the results, i submitted and then viewed the results and it was error free |
| QUOTE (joe2kiss @ Mar 13 2004, 02:28 PM) |
| great tutorial m8 im going to see if i can change the way the results are displayed now lol |
| QUOTE (Josh @ Mar 13 2004, 04:05 PM) |
| Hi timo, couple questions on how it works. What does the variable $r do? And what is the part in red for: echo '<form method="post" action="'.$_SERVER['SCRIPT_NAME'].'" />'; |
| QUOTE |
| Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /var/www/scottydog.qc1.net/poll/index.php on line 84 |
| QUOTE |
| Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /var/www/scottydog.qc1.net/poll/ratings.php on line 37 |
| QUOTE (Timo @ Apr 6 2004, 01:54 PM) |
| Joe if you're just editting an old version you'll have to change the $_POST[''] lines as well... |
| QUOTE |
| <?php function dbinsans($text) { $text = strip_tags ($text, ""); $text = str_replace(chr(10),"",$text); $text = str_replace(chr(13), "<br>", $text); $text = str_replace("\"","*",$text); $text = str_replace("'","*",$text); $text = addslashes($text); return($text); } ?> |
| QUOTE (Bushido @ Jul 8 2004, 12:52 PM) |
| hey timo, do you think you can put thoses together in a zip file for me. Please. Thanks. |