Easy Surveys and Polls with a Movable Type Weblog


Published on February 27, 2006 at 5:49 AM EST
In the Tutorials category.

A tasty dessert filled with chocolate, caramel, and PHP-goodness, smothered in fresh HTML and CSS

Update: A better way to add polls is with the Poll Position plugin!

An easy way to integrate a survey system with Movable Type is something I’ve wanted for a while. Learning Movable Type’s Polls and Voting article describes two methods of adding such things to a website, but it doesn’t really integrate. Clearly, a better solution is needed.

This tutorial will help you create an MT-based survey and polling system that uses the familiar Edit Entry screen and comments to store the votes. Using a separate weblog, some PHP, and these templates, you can have your own survey/polling/voting system in place quickly and easily!

You might want to try participating in a survey to see how it works, too.

Background

If you’re not interested in how I “got here,” you can just skip by this section and get started.

When I was first thinking about building this website, I wanted to make it stand out from the other Movable Type community websites. I had two ideas of how I wanted to do that: with a unique style (which became this restaurant theme) and frequent, fun surveys.

The Polls and Voting article on Learning Movable Type provided me with some quick and easy ways to set up surveys, but nothing that really integrated well with MT. A while later the book Hacking Movable Type came out with a chapter devoted to creating a weblog for polls, complete with the code to make it work.

Unfortunately, I could never get the templates from Hacking MT to work, despite fooling with it on and off for several months—and this site was missing a key element to make it fit my original vision. Then, one day, it ocurred to me that using some PHP, I knew exactly how to build a simple polling system. About an hour later I had this system worked out!

Create a New Weblog

Surveys need their own weblog to work, so you’ll need to create a new one. Create a new weblog (from the Main Menu, click Weblogs and Create New Weblog, then give it a name and directory). After saving changes, go to your new weblog’s Settings screen.

  • In the New Entry Default Settings screen you’ll need to change Text Formatting to None and be sure Accept Comments is checked, then save changes.
  • In the Feedback Settings screen, choose Accept Comments from: Anyone. Do not check the Require E-mail Address box. Choose to Immediately Publish Comments from: Anyone. Lastly, Text Formatting should be set to None, and save the changes.
  • In the Publishing Settings screen change the File Extension for Archive Files option from html to php and save your changes.

Update Templates

The templates are really the core of what makes this survey system work. Below are the updated templates. Copy and paste them into the appropriate template type; be sure to save each one.

Main Index

Category Archive

Date-Based Archive

Individual Entry Archive

Creating a Survey

http://www.eatdrinksleepmovabletype.com/upload/2006/02/new-entry-thumb.gif

Just as you’re probably used to, go to the New Entry screen to create a survey. You can refer to the screenshot at the right, if it helps.

  • Add a title and, if you want, a category.
  • The Entry Body field should contain your survey question. Because Text Formatting is set to None, you must enclose your text in HTML. If you’re not familiar with HTML, just follow this example to add the paragraph (<p></p>) tags:
    <p>What is the survey question?</p>
  • The Extended Entry field holds the voting options. Put each option on a new line. You can use some HTML to format the options, but do not wrap them in paragraph tags.
  • Publish and Save!

To end the “voting period” go back into this entry and uncheck the Accept Comments checkbox near the bottom of the screen.

A Little More Detail

This survey system works by assigning a number to each voting option. When somebody votes they’re actually making a comment through MT’s commenting system, and this comment is a number. Those numbers get added and tallied to create the survey results.

Voting is allowed or disallowed based on the visitors IP address. It’s not a perfect system, but it was easy to implement.

I’ved added two little pieces of easy customization to the survey templates. At the top of the Individual Entry Archive templates are two variables you can change: $bar_width and $colors.

  • $bar_width is the width of the bar graph that gets created for the results, and is specified in pixels.
  • $colors is an array of the colors to use for the bar graph. There must be at least as many colors listed as their are options for your survey. There are currently 10 colors listed, so your surveys shouldn’t contain more than 10 options (of course, fewer is fine). If you want to have more options, you’ll want to add more colors—just follow the format established.

This article is tagged as: Comments, CSS, HTML, PHP, Surveys, Template, Tutorials