Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build Your Own PHP Survey Engine : Page 3

Building interactive features into your Web site, such as a live poll or survey, has never been easier. Follow along step-by-step as you learn to build a PHP-based survey engine with an RDBMS on the back end and HTML histogram data on the front.


advertisement
Rendering the Results
The same file (process.php) will also render the results of the poll. There are a number of steps in this process:
  1. You need to know how many total answers the question has.
  2. You need to count how many of each option have been answered. So, if there are 100 answers spread over three options, you need to know how many are for option 1, for option 2, etc.
  3. You need to calculate the percentage response for each of the answers.
  4. You need to draw the results as an histogram.
First, query the database to count the number of records for a specific question from the answers table:

$SQL = "SELECT Count(QuestionID) FROM Answers WHERE QuestionID=" . $QID; $result = mysql_query($SQL); $row = mysql_fetch_row($result); $nTotalVotes=$row[0];

This is as straightforward as doing a SQL COUNT query and getting the results. As you are only selecting one field—Count(QuestionID)—the results will be in $row[0].

The next step is to get the total number of votes for each option. In addition, you want to get the text associated with that option (to make the histogram more meaningful), so you need to do a SQL count query on the grouped answers for this question and join it to the Options table on the answer value:



SELECT answers.answervalue, options.optiontext as tx, count(answers.answerValue) as ct FROM answers, options WHERE answers.answervalue = options.optionvalue AND answers.QuestionID=1 GROUP BY answers.answervalue

Figure 4. Results of the Poll. The results are queried using SQL and drawn as a histogram.
Now that you have the results, you want to draw them out as a chart (see Figure 4). You can do this with PHP's built-in image functionality. Start by informing the browser that the page is a GIF so that it can handle it properly. This is done at the top of process.php with the following command:

Header("Content-Type: image/gif");

Next, draw the actual image itself:

$xDim = 400; $yDim = 15+(mysql_num_rows($result)*15); $im = ImageCreate($xDim, $yDim); $backg = ImageColorAllocate($im, 255,255,200); $blue = ImageColorAllocate($im, 0, 0, 255); $nY = 0; while($row = mysql_fetch_array($result)) { $percVote = round(($row['ct']/$nTotalVotes)*100,2); $percWidth = ($percVote * $xDim / 100) - 10 ; ImageFilledRectangle($im, 0, $nY+1, $percWidth, $nY+10, $blue); ImageString($im,2,$percWidth + 5, $nY+1, $row['tx'] . " " . $percVote . "%", $blue); $nY=$nY+15; } ImageGIF($im);

The ImageCreate command is used to create an image in memory with the specified height and width. In this case the width is set to 400 ($xDim=400) and the height is calculated based on the number of options for this question—that is, the number of rows in the resultset for the SQL query from the previous section (using mysql_num_rows).

Next, a couple of colors are allocated, using the ImageColorAllocate command. The first time this is called, the background of the image is set. The second time it is called, a reference to the color is set to the variable that stores its response. So, in this case $blue will contain a reference to the color blue (using RGB values 0,0,255).

Finally, the script loops through the recordset and for each record calculates the width of the bar in the histogram based on the percentage it received in the vote tally. It uses the ImageFilledRectangle command to draw a filled rectangle of the appropriate color, whose width is based on this value. It uses the ImageString to place text beside the rectangle. The text is the text for the option followed by the percentage representing that vote.

Finally, once the loop through the records is complete it uses the ImageGIF function to output the image as a GIF file.

As you can see, building a simple survey poll for interactivity on your Web site is very easy using PHP. The built in database drivers for MySQL and graphics drivers for GIF, JPEG, or other formats are enormously useful. In this sample you built a very simple, one-step poll, but it gives you the foundation upon which you can built more complex and interesting applications. And most importantly, will let you see into the heads and hearts of your customers and visitors.



Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date