iD NEWS & BLOG
Making a Poll in PHP from Scratch: Part 1
There are plenty of ready-made Internet polls out there, particularly if you are using a framework for your site such as Wordpress, vBulletin, or Joomla/Drupal. But occasionally it is worthwhile to create your own from scratch, whether for learning or to simply fulfill specific features you need in your application. In this series of posts, that is exactly what we will create – a homemade PHP/MySQL voting page. You should be then able to modify this code for your specific needs.
First, let’s talk about the inherent flaws in Internet voting. In the most basic form, an Internet poll is just a choice a person makes on a page that is stored in the database. Without any checks, a person could just refresh the page and vote over and over again. Or, worse, they could employ a script of their own that does this automatically. You could potentially register millions of votes this way, given enough time. So what checks and balances do we have? There are three main options.
User Login
A user login system is the most secure of your options, especially if the accounts are linked to a unique email address. If you are using a system like Wordpress or Drupal, then it is trivial to store the userid of the voter in the database as well. Do you want to use this option with your poll? If the user is already logging in for other reasons, then this is probably fine. Making a user register just to vote in a poll brings the convenience factor way down, most likely deterring most people. Plus, is it really worth it? If a user has the time and inclination, they can simply register over and over again, especially if they have their own domain or email server from which to pull infinite accounts. You could employ some IP-checking with your account checks to make sure they are unique, but then it just becomes an arms race. This option is too burdernsome for us to use in this project.
Cookies
We could use cookies set on a person’s machine to determine whether they’ve voted or not, but all it takes is a browser switch, cleared cache, or a privacy mode and suddenly our checking goes right out the window. This option is almost too simple for the user to overcome.
IP Address
The last major option is an IP address check. There are several flaws with this method, like the others, but we can overcome a few of them more easily. First, IP addresses are not a one-address-to-one-person mapping. The system can go either way, in fact. You can have multiple IPs for a single person, if they have multiple systems, a cellphone, so on. You can also have multiple people for an IP, in the case of a corporate network. You could have 10,000 people registering as a single IP. But these are problems we can find workarounds for. We’ll use IP checking in conjuction with a few tricks as our authentication method.
Database Structure: Your Favorite Fruit
For this poll, I’m creating two different database tables, mypoll and mypolltally. The first table will contain all of the choices in our poll and the second the individual votes. This means that we can only have a single poll at a time, but this is a trivial matter in the future to extend (we would simply add another column to mypoll, pollid, containing a unique identifier. Then we would just need a column in mypolltally to store the pollid of each vote).
mypoll mypolltally -------------------------- -------------------------------------------- | id | choice | | id | isvotefor | ipaddress | value | -------------------------- -------------------------------------------- | 0 | banana | | 0 | 3 | 255.255.255.255 | 1 | | 1 | apple | | 1 | 4 | 254.255.255.255 | 1 | | 2 | orange | | 2 | 1 | 251.255.255.255 | 1 | | 3 | strawberry | | 3 | 2 | 252.255.255.255 | 1 | | 4 | blueberry | | 4 | 2 | 253.255.255.255 | 1 | -------------------------- --------------------------------------------
The Fields
mypoll:
- id: A unique identifier for records in the table. When we register a vote in mypolltally, we’ll need to know which option we are voting for. We’ll use this value to do so.
- choice: The text for each option. Our poll question is “What is your favorite fruit?”, so here we lay out the choices such as
- banana, apple, and orange.
mypolltally:
- id: A unique identifier for each vote in the system.
- isvotefor: This field points identifies which option in mypoll that this vote is selecting.
- ipaddress:The IP address of the voter.
- value:We can extend our features a little by having this field. Instead of simply registering a “Yes” for a particular option, we can register a range. We can then allow people to indicate how much they like the option. Perhaps a 1 value is a little, a 5 value is a lot, and anything negative means they do not like it. For now, a value of 1 will just mean yes but later we’ll extend it for enhanced voting.
That’s enough of the theory for now. In the next part of the series we’ll launch into the actual PHP code and finish up with the basic functionality. In Part 3 we’ll extend our code to allow for more features.
Tags: internet poll, internet polls, internet voting, mysql, PHP, php mysql, php poll, poll, voting
Posted in iD Tech Bloggers | No Comments »
Integrating Flickr with vBulletin
Our support forums here at iD Tech run on the popular internet forum software, vBulletin. Each year we constantly revamp the system with new upgrades, a new design, and new code plugins to make the process more specific to our business model and more accessible to our staff.
In addition, we also have a public Flickr stream. I wanted to pull these two elements together so that we could bring examples of our Flickr photos to our own support system. Here’s how I integrated them.
- Download the phpFlickr API from here: http://www.phpflickr.com/
- Unzip the files to your vBulletin /includes/ folder.
- If you want, you can rename the files to match the vBulletin naming convention (i.e. phpflickr.php -> functions_phpflickr.php).
- Optional: Create a new vBulletin product. From the admin side, choose Manage Products from the Plugins & Products menu. Then select Add/Import product. Fill in the information you need for a new product and hit save.
- Add a new vBulletin plugin. From the Plugins & Products admin menu, select Add New Plugin. Select the product you created in Step 4, or vBulletin otherwise. For hook location, you’ll need to select where you want the Flickr photos displayed. I’m placing them on our vBulletin homepage, so I select forumhome_start for my hook. Enter a title (such as Flickr Integration), leave the exectution order alone, and use the PHP code shown below.
- From the Styles & Templates menu, select Style Manager. Pick your style and choose Edit Templates. Find FORUMHOME (or whichever style is applicable to the hook location you chose) and double-click on it.
- Find the location in the template where you want your photos displayed and enter $flickrbox, the name of the variable we are pulling from our PHP plugin.
Now let’s take a look at the code we’re placing in our plugin.
require_once("includes/phpFlickr.php");
// Create new phpFlickr object
$f = new phpFlickr("yourapikey");
$f->enableCache("db","mysql://user:password@server/database");
$limit = 11;
// Find the NSID of the username inputted via the form
$person = $f->people_findByUsername('iDTechCamp');
// Get the friendly URL of the user's photos
$photos_url = $f->urls_getUserPhotos($person['id']);
// Get the user's first X($limit) public photos
$photos = $f->people_getPublicPhotos($person['id'], NULL, NULL, $limit);
$flickrbox = "<table class='flickrbox'><tr>";
// Loop through the photos and output the html
foreach ((array)$photos['photos']['photo'] as $photo) {
$flickrbox .= "<td><a class='flickrimg' href=$photos_url$photo[id]>";
$flickrbox .= "<img border='0' alt='$photo[title]' "
."src=" . $f->buildPhotoURL($photo, "Square") . ">";
$flickrbox .= "</a></td>";
}
$flickrbox .= "</tr></table>";
First, we require_once the phpflickr php api library. Alter this line if you choose to name your library differently. Next, we create a new phpFlickr object, $f, and supply our Flickr API key. Change this line to reflect your unique API key. Next, we optionally enable the cache available with this library by supplying our mySQL database details. If you don’t have your details, don’t worry, this step isn’t required.
We then set a limit for how many photos we want. I’ve chosen 11. We use people_findByUsername to get photos for our particular user, in this case, the company username iDTechCamp. The resulting statements will pull $limit number of photos from the public profile of our user. Lastly, we need to construct a variable that holds the output as HTML. Our variable here is $flickrbox and as you can see I’m generating a table with CSS class style named flickrbox. Each image in the table is table cell and contains a link to that particular image. We also set the image title to the photo title pulled from Flickr.
You can easily alter the PHP code here to produce tableless HTML or any other structure you like. I hope these instructions help you to integrate your board and Flickr! Email me or comment with any questions you might have.
Tags: api, changes, Classes, code, css, Design, Flickr, Flickr API, Manager, mysql, Objects, photo, photos, PHP, phpFlickr, style, tech, vBulletin
Posted in iD Tech Bloggers | 2 Comments »
Populating a Combo-box in PHP Dynamically from MySQL
For this article, let’s pretend you have the following database table. There are two columns (I’m separating the fields with commas for readability), Item and Price:
Cheese Pizza, 1.00
Pepporoni Pizza, 1.50
Sausage Pizza, 1.50
Cheese Calzone, 1.50
Ham Calzone, 2.00
What if you want to populate a combo-box with your possible options? We can write php that generates a drop-down box that has five item choices in it, based on our table above. Here is the sample code:
<?php
// Connect to the database
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_select_db("name") or die(mysql_error());
// Has the form been submitted?
if (isset($_POST['item'])) {
// The form has been submitted, query results
$queryitem = "SELECT * FROM table WHERE item = '".$_POST['item']."';";
// Successful query?
if($result = mysql_query($queryitem)) {
// More than 0 results returned?
if($success = mysql_num_rows($result) > 0) {
// For each result returned, display it
while ($row = mysql_fetch_array($result)) echo $row[serial];
}
// Otherwise, no results, tell user
else { echo "No results found."; }
}
// Error connecting? Tell user
else { echo "Failed to connect to database."; }
}
// The form has NOT been submitted, so show the form instead of results
else {
// Create the form, post to the same file
echo "<form method='post' action='example.php'>";
// Form a query to populate the combo-box
$queryitem = "SELECT DISTINCT item FROM table;";
// Successful query?
if($result = mysql_query($queryitem)) {
// If there are results returned, prepare combo-box
if($success = mysql_num_rows($result) > 0) {
// Start combo-box
echo "<select name='item'>\n";
echo "<option>-- Select Item --</option>\n";
// For each item in the results...
while ($row = mysql_fetch_array($result))
// Add a new option to the combo-box
echo "<option value='$row[item]'>$row[item]</option>\n";
// End the combo-box
echo "</select>\n";
}
// No results found in the database
else { echo "No results found."; }
}
// Error in the database
else { echo "Failed to connect to database."; }
// Add a submit button to the form
echo "<input type='submit' value='Submit' /></form>";
}
?>
Tags: code, combo-box, drop-down box, mysql, PHP
Posted in iD Tech Bloggers | 3 Comments »





