Thumbs Up or Thumbs Down

There are many ways to write a script that will allow your site visitors to simply say "yes i liked this" or "no I do not like this" with a catchy thumbs up or down image. Here is a way to do it with "yes" or "no" buttons that will tally up the results. First we need to set up the database so that we can store what we are "liking". Let's take an example of link sharing site, and you just want to see if people like or dislike a particular link. (You can also just use phpMyAdmin if you like, but here is the SQL to do it as well).
CREATE TABLE  `your-database`.`links` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`url` VARCHAR( 100 ) NOT NULL ,
`title` VARCHAR( 100 ) NOT NULL
) ENGINE = MYISAM ;
Now we need to create the table that will hold the "likes and dislikes"
//We are going to populate the "id" field with the "id" of the link to hook the "like" up with the correct link
CREATE TABLE  `your-datable`.`likes` (
`id` INT( 5 ) NOT NULL ,
`like` VARCHAR( 1 ) NOT NULL
) ENGINE = MYISAM ;
Now we need to present the link your site visitor with the "like/dislike" options.
function displaylinks($input) {

echo $input['title'] . "<br>";
echo "<a href="http://" . $input['url'] . "">" . $input['url'] . "</a>";

?>

Like This?<br><a href="saveaddlike.php?like=<? echo $input['id']; ?>">YES</a> |
<a href="saveadddislike.php?like=<? echo $input['id']; ?>">NO</a>
<span style="font-size: 5px; line-height: 5px;"><br><br></span>

<?

//We would like to show the current amount of likes and dislikes
positivelikes($input['id']); negativelikes($input['id']);               

}
Here is the code for the positivelikes() function...the same goes for the dislikes just replace the "y" with a "n" in the select statement.
function positivelikes($id) {

$select = mysql_query( "SELECT likes, COUNT(id) FROM likes WHERE likes = 'y' AND id = '$id' GROUP BY likes" ) or die("SELECT Error: ".mysql_error());
while ($row = mysql_fetch_array($select)) { 

echo "" .$row['COUNT(id)'] . "</div>";

        }
}
Here is the code for saveaddlike.php You can put the code for savedislike.php in here and have a conditional statement for "$like" but for the sake of this article, i think this works just fine.
$likeid = $_ GET['like']
$like = "y";
$insertlike = "INSERT INTO likes (id, likes) VALUES('$likeid', '$like')";
mysql_query($insertlike) or die(mysql_error());

$referer_url = $_SERVER['HTTP_REFERER'];
header('Location: ' . $referer_url);
Next we need a function that will put all this together
function full_link_list() {

$select = mysql_query( "SELECT * FROM links" ) or die("SELECT Error: ".mysql_error());

while ($row = mysql_fetch_array($select)) {
displaylinks($row);
}
}
You can save the following functions into a library.php file displaylinks(); positivelikes(); negativelikes(); full_link_list(); View Live Demo I have done some styling to make this look better and easier to demo I have a function called "buildpage()" in my library that gets an html header and the full_link_list()

function buildpage() {

get_header();
full_link_list();

}
And then I call "buildpage()" from my index.php file which looks like this...
<? require_once "/path/to/my/library.php"; buildpage(); ?>
Post a comment





Real Time Web Analytics ^