You are on page 1of 4

Pulse Votes

An easy-to-integrate PHP Voting Script


(Demo Installation)

Let’s say, we have a demo database called pulse_blog_demo. Let’s assume we have a table
that contains posts of a blog. It’s called posts. There are three fields for the posts table. They
are
1. id — the unique id of each post. It is set to be primary key. It’s also non-null.
2. title — the title of each post. We have set it to varchar with a length of 255.
3. post_body — the main body of the post. We have set it to text.
In case you need the SQL to create the table, you can use the following:
CREATE TABLE `posts` (
`id` int(11) not null auto_increment,
`title` varchar(255),
`post_body` text,
PRIMARY KEY (`id`)
)
Now, we insert some dummy data in it. Once we are done with that, we create an index.php
file to show the posts. But before that we need to connect to the database. Our config.php file
will do this.

<?php
$hostname = "localhost";
$username = "YOUR_USERNAME";
$password = "YOUR_PASSWORD";
$dbname = "YOUR_DB_NAME";
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname) or die("Unknown database!");
?>

Of course, you would have to change YOUR_USERNAME, YOUR_PASSWORD and YOUR_DB_NAME


We now, create the index.php file.

<?php include("config.php"); ?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Blog Posts</title>
<link rel='stylesheet' href='style.css'></link>
</head>
<body>
<div id='wrapper'>
<ul>
<?php
$q = "SELECT * FROM posts";
$r = mysql_query($q);
while($row = mysql_fetch_array($r)):
?>
<li>
<h2><a href=''><?php echo $row['title']; ?></a></h2>
<div class="post-body">
<?php echo $row['post_body']; ?>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
</body>
</html>

This is pretty simple. First we include the file connect.php to connect to our database. Then,
we get all the records of the posts by SELECT * FROM posts. Then we loop through each
row of the post table and show the title and post_body.
For the sake of the demo installation, we create a style.css file.

html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p,
blockquote,
pre, form, fieldset, table, th, td { margin: 0; padding: 0; }
body {
font-size: 14px;
line-height:1.3em;
text-align:center;
}
a, a:visited {
outline:none;
}
.clear {
clear:both;
}
#wrapper {
width:960px;
margin:0 auto;
text-align:left;
}
ul {
list-style: none;
}
li {
margin-bottom:5px;
background:#E3E9FF;
border:1px solid #BBCCFF;
}
h2 {
font-family:Georgia;
font-size:1.1em;
background: #BBCCFF;
padding:4px;
}
h2 a {
color:#000000;
}
.post-body {
padding:6px;
}

Now, we integrate Pulse Votes to our demo blog-posts. Download Pulse from http://
s.technabled.com/PulseVote. Unzip the file. Inside the Pulse directory, there’s file called
pulse.config.php. We need to edit the file to install it.
1. Change the value of PULSE_DIR constant to the absolute path of the directory
where you’re going to upload the Pulse directory. For instance, if you are planning to
upload it to libs directory under your htdocs directory, then, it should be http://
yourdomain.tld/libs/Pulse
2. Also, change the values of HOSTNAME, USERNAME, PASSWORD with your database
credentials. For this demo, our DATABASE name is pulse_blog_demo
Now, we need to change one more file. Inside the assets directory, there’s js directory.
Inside it is pulse.core.js file. Edit the line that starts with var u. Set the value of u to the
same value as PULSE_DIR. (i.e. http://yourdomain.tld/libs/Pulse)
Now, we run the following SQL to create the pulse_votes table which contains the votes.
CREATE TABLE `pulse_votes` (
`id` int(11) not null auto_increment,
`item_id` int(11),
`vote_value` int(11),
`ip` varchar(255),
`date` timestamp not null default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
Now, we upload everything.
Next, we get back to our index.php file. After the line where we included config.php, we add
the following line to include Pulse.vote.class.php
<?php include("Pulse/Pulse.vote.php"); ?>
Then, we add the following lines before the html tag
<?php
$pulse = new Pulse();
$pulse->setFormat("{up} like it, {down} don't");
?>
The first line creates new instance of the Pulse object. The second line sets the format in
which we like the result to show up. Here {up} will be replaced by the number of upvotes and
{down} by the number of downvotes. You can also use {balance} to show the difference
between upvotes and down votes.

Then, under the head tag, we add these two lines include the required CSS and JavaScript files.
<?php echo Pulse::css(); ?>
<?php echo Pulse::javascript(); ?>

Now that we are done with including the CSS and JS files, we add the following line inside the
post-body class in index.php file (or wherever you want, but it must be inside the loop).
<?php echo $pulse->voteHTML($row['id']); ?>
This line prints the necessary HTML to show the thumbs for the votes. Note that the voteHTML
method takes only one parameter and it is an integer.

And that’s all!

You might also like