Version: 1.0
Type: Full Script
Category: Voting
License: GNU General Public License
Description: We originally wrote this “voting system” to rate Vendors in a banner-sharing system on a scale from one to ten.
You’ll need to add an SQL database called “vendors” with a Drop Table called “RECORDS” having these fields (in order):
ID = int autoincrement primary key
Name = varchar (32)
Description = varchar(255)
VoteCount = int(11)
VoteTotal = int(11)
Now create some records in the database… of course it doesn’t have to be “vendors”, use anything you want to rate and track on a scale from one to ten.
Remeber to configure the script with your correct login and password,
This script is really stripped down, so you can see how it works… but it is fully functional. Perhaps you will want to add a “Thank You” screen after a user has completed voting, right now it simply retabulates and shows the new totals. If you have many items to be voted on, you may want to add a “next” and “prior” button setup. Finally, you’ll almost certainly want to pretty it up to match your web page scheme.
You may contact the author at [email protected]
Enjoy!
<?php // // Vendor Ratings System 1.0 // Copyright (C) 2000, AtoZnet.com // Author: Clayton McGow // [email protected] // // You may use and alter this code, but do // not distribute altered code without // adding a comment section similar to this // one stating the nature of the changes made // and do not remove anything above this line. // // The purpose of this script is to rate // "items" on a scale from one to ten. // The result of each vote is counted, // tabulated, averaged, and displayed in // the resulting html code. // You must add an SQL database called // "vendors" with a Drop Table called // "RECORDS" having these fields (in order): // ID = int autoincrement primary key, // Name = varchar (32), Description = // varchar(255), VoteCount = int(11), // VoteTotal = int(11). // Now add some records to the database // for your surfers to "vote" on. // ?> <html> <head> <title>title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <FORM method="post" action="<?php echo $PHP_SELF?>"> <?php $db = mysql_connect("localhost", "userID","password"); // don't forget to customize this line mysql_select_db("vendors",$db); // you must create the database called "vendors" if ($submit) { // if they have just voted, // process the new info from the form while (list($name, $value) = each($HTTP_POST_VARS)){ // by stepping through the all the args if ($name!="submit"){ // except the submit button. if ($value!=0){ // If they voted on a particular item, // tally up the new total score // and increment the number of // votes counted for that item $theQuery=sprintf("%s%s ","SELECT * FROM RECORDS where ID=", $name) ; $result= mysql_query($theQuery,$db); $myrow = mysql_fetch_row($result); $VoteCount=$myrow[3]; $VoteCount++; $theQuery=sprintf("%s%d%s%d ","UPDATE RECORDS SET VoteCount=",$VoteCount ," where ID=", $name); $result = mysql_query($theQuery,$db); $oldVoteTotal= $myrow[4]; $newVoteTotal=$oldVoteTotal+$value; $theQuery=sprintf("%s%d%s%d ","UPDATE RECORDS SET VoteTotal=",$newVoteTotal ," where ID=", $name); $result = mysql_query($theQuery,$db); } // end if value !=0 } // end if $name!=submit } // end while list http_vars } //end if submit $result = mysql_query("SELECT * FROM RECORDS",$db); while ($myrow = mysql_fetch_row($result)) { // list all the data // for every item in the "RECORDS" table printf("<p>ID: <b>%d</B><br>Name: %s<br>Description: %s<br>VoteCount: %s<br>VoteTotal: %s</p>n", $myrow[0], $myrow[1], $myrow[2], $myrow[3], $myrow[4]); // the next line divides VoteTotal by VoteCount // and shows is as a "floored" or rounded down int // unless noone voted on it yet - avoid the // division by zero condition if ($myrow[3]!=0) printf("<p>The average score for this vendor is %d </p>n", $myrow[4]/$myrow[3]); else printf("<p>This vendor has not received any votes yet.</p>n"); // the following html code offers a chance for // the surfer to vote on each item in the database // by presenting a row of radio buttons from one to ten. // Each radio button is given the same name as the // "ID" field of the item in question... in this case // it'll be the same as the record number ?> <p><b>Now <u>you</u> rate this vendor!</b></p> <table width="75%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="8%"> <div align="center">Worst </div> </td> <td width="8%"> <div align="center">1 <input type="radio" name="<?php echo($myrow[0]) ?>" value="1"> </div> </td> <td width="8%"> <div align="center">2 <input type="radio" name="<?php echo($myrow[0]) ?>" value="2"> </div> </td> <td width="8%"> <div align="center">3 <input type="radio" name="<?php echo($myrow[0]) ?>" value="3"> </div> </td> <td width="8%"> <div align="center">4 <input type="radio" name="<?php echo($myrow[0]) ?>" value="4"> </div> </td> <td width="8%"> <div align="center">5 <input type="radio" name="<?php echo($myrow[0]) ?>" value="5"> </div> </td> <td width="8%"> <div align="center">6 <input type="radio" name="<?php echo($myrow[0]) ?>" value="6"> </div> </td> <td width="8%"> <div align="center">7 <input type="radio" name="<?php echo($myrow[0]) ?>" value="7"> </div> </td> <td width="8%"> <div align="center">8 <input type="radio" name="<?php echo($myrow[0]) ?>" value="8"> </div> </td> <td width="10%"> <div align="center">9 <input type="radio" name="<?php echo($myrow[0]) ?>" value="9"> </div> </td> <td width="8%"> 10 <input type="radio" name="<?php echo($myrow[0]) ?>" value="10"> </td> <td width="10%"> <div align="center">Best </div> </td> </tr> </table> <hr> <?php // now finish up the html form with // a "submit" button, and close the session } // end while (from up above the table html code) echo ("<input type="submit" name ="submit" value ="submit">n"); echo ("</form>n"); MYSQL_CLOSE(); ?> </body> </html>