PHPBuilder - Creating an Online Survey - Part 2, Page 2



RSS Twitter
Articles Databases

Creating an Online Survey - Part 2, Page 2

by: PHP Builder Staff
|
March 27, 2012

At this point we've gotten the form data into our database, and have incremented the totals based upon the responses. Since all of the data collecting is done, why not give the respondant the results? We'll re-create the input table they just saw, only this time we'll give the totals instead of the radio buttons.

The SQL query is fairly simple--we're just grabbing all the information from the one row we have, and displaying it in the appropriate spot. Here's the query:


$sql3="SELECT * FROM `onlinesurvey_totals`";
$rslt3=mysqli_query($dbc, $sql3);
$row=mysqli_fetch_array($rslt3);

All we have left to do is to create the table and display the information. As you can see, that code is fairly straight-forward:


echo '<table border="1" style="text-align: center;">
<tr>
	<td colspan="6">Site Survey Statistics</td>
</tr>
<tr>
	<td>What gender are you?</td>
	<td colspan="3"><b>Male:</b> '.$row['male'].'</td>
	<td colspan="2"><b>Female:</b> '.$row['female'].'</td>
</tr>
<tr>
	<td>What is your age group?</td>
	<td colspan="5">
		<table border="1" style="text-align: center;">
			<tr>
				<th>0 - 8</th>
				<th>9 - 16</th>
				<th>17 - 25</th>
				<th>26 - 32</th>
				<th>33 - 40</th>
				<th>41+</th>
			</tr>
			<tr>
				<td>'.$row['0_8'].'</td>
				<td>'.$row['9_16'].'</td>
				<td>'.$row['17_24'].'</td>
				<td>'.$row['25_32'].'</td>
				<td>'.$row['33_40'].'</td>
				<td>'.$row['41_x'].'</td>
			</tr>
		</table>
	</td>
</tr>
<tr>
	<td>Was this phone or email support?</td>
	<td colspan="3"><b>Email:</b> '.$row['email'].'</td>
	<td colspan="2"><b>Phone:</b> '.$row['phone'].'</td>
</tr>
<tr>
	<td>What is your knowledge with computers?</td>
	<td><b>Little:</b> '.$row['little'].'</td>
	<td><b>Some:</b> '.$row['some'].'</td>
	<td><b>A Lot:</b> '.$row['lots'].'</td>
	<td><b>Expert:</b> '.$row['expert'].'</td>
	<td></td>
</tr>
<tr>
	<td colspan="6" style="height: 10px; background: #ccc;"></td>
</tr>
<tr>
	<td></td>
	<th>Very Dissatisfied</th>
	<th>Dissatisfied</th>
	<th>Satisfied</th>
	<th>Very Satisfied</th>
	<th>Neutral</th>
</tr>
<tr>
	<td>Overall how satisfied are you with the support you received?</td>
	<td>'.$row['1_vd'].'</td>
	<td>'.$row['1_d'].'</td>
	<td>'.$row['1_s'].'</td>
	<td>'.$row['1_vs'].'</td>
	<td>'.$row['1_n'].'</td>
</tr>
<tr>
	<td>How satisfied are you with the speed of the support?</td>
	<td>'.$row['2_vd'].'</td>
	<td>'.$row['2_d'].'</td>
	<td>'.$row['2_s'].'</td>
	<td>'.$row['2_vs'].'</td>
	<td>'.$row['2_n'].'</td>
</tr>
<tr>
	<td>How satisfied are you with the knowledge of the support representative?</td>
	<td>'.$row['3_vd'].'</td>
	<td>'.$row['3_d'].'</td>
	<td>'.$row['3_s'].'</td>
	<td>'.$row['3_vs'].'</td>
	<td>'.$row['3_n'].'</td>
</tr>
<tr>
	<td>How satisfied were you with the result of the support?</td>
	<td>'.$row['5_vd'].'</td>
	<td>'.$row['5_d'].'</td>
	<td>'.$row['5_s'].'</td>
	<td>'.$row['5_vs'].'</td>
	<td>'.$row['5_n'].'</td>
</tr>
<tr>
	<td colspan="6" style="height: 10px; background: #ccc;"></td>
</tr>
<tr>
	<td></td>
	<th>Not Very</th>
	<th>Somewhat</th>
	<th>Neutral</th>
	<th>Very</th>
	<th>Extremely</th>
</tr>
<tr>
	<td>How would you rate the personability of the support representative?</td>
	<td>'.$row['6_nvp'].'</td>
	<td>'.$row['6_sp'].'</td>
	<td>'.$row['6_p'].'</td>
	<td>'.$row['6_vp'].'</td>
	<td>'.$row['6_ep'].'</td>
</tr>
<tr>
	<td colspan="6" style="height: 10px; background: #ccc;"></td>
</tr>
<tr>
	<td></td>
	<th colspan="3">Yes</th>
	<th colspan="2">No</th>
</tr>
<tr>
	<td>Would you recommend us to a friend or co-worker for support 
requests in the future?</td>
	<td colspan="3">'.$row['4_yes'].'</td>
	<td colspan="2">'.$row['4_no'].'</td>
</tr>
</table>';

Let's not forget to close the mySQL connection, and finish out our script:


mysqli_close($dbc);
}
}
?>

And that's it!! The full source for all examples can be viewed here:
Database Version
Email Version

Conclusion

Over the course of this article we have looked at how to set up a form and submit it. We also looked at how to email the results to an address, or submit them to a database and display the results. With this knowledge, the new PHPer should be able to set up their own online survey and record the results.

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Comment:



Comment:

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