PHP - View Survey Results

 

This lesson is part of an ongoing Survey/Poll tutorial. The first part is here: Build your own Survey/Poll, along with all the files you need.

If you remember from the survey page, we had button that said "View Results". This button was part of a form. The form had a HTML hidden tag. This hidden tag had the ID number for a row in our database table. When the button is clicked, the form passes this hidden ID number of to the viewResults.php page. The viewResults page uses this row ID to pull records from the table. Here's all the PHP code for viewResults:

PHP code for survey results (opens in a new browser tab as a text file)

Here is the page itself, when you click the button:

Web page showing results of survey

At the very top of the code, we've set up quite a lot of variables. Ten of those variables are for the red horizontal bars you see in the image above. The other variables will hold information from the database table. The first real bit of code is the if statement:

if (isset($_GET['Submit2']) && isset($_GET['h1'])) {
}

This checks if two things are set: the Submit button from the survey form, and the hidden variable, which we've called h1. If they are, we can GET the value of h1:

$qNum = $_GET['h1'];

After connecting to the server and database, we have this for our SQL:

$SQL = $db_found->prepare('SELECT * FROM tblsurvey WHERE ID = ?');

This time, we do want all the records from the table. But only WHERE we've found a matching ID field.

You can actually test to see if the SQL is OK. You do it with an if statement:

if ($SQL) {
}

This will return true if the SQL is OK, and false if it's not.

Inside the SQL test, we have bind the parameters and execute:

$SQL->bind_param('i', $qNum);
$SQL->execute();

Because the ID field is an integer in our table, we need the letter i as the first parameter between the round brackets of bind_param. The $qNum variable hold the actual ID.

Next, we try to get some results back:

$result = $SQL->get_result();

if ($result->num_rows > 0) {
}

If we successfully return some rows, we can get an array of values from the database table:

$db_field = $result->fetch_assoc();

Now we can go ahead and fill our variables that we set up at the top of the code:

$question = $db_field['Question'];

$answerA = $db_field['OptionA'];
$answerB = $db_field['OptionB'];
$answerC = $db_field['OptionC'];

$qA = $db_field['VotedA'];
$qB = $db_field['VotedB'];
$qC = $db_field['VotedC'];

Every field from our table is accessed and placed into variables. We need all of these values back, as we're going to be displaying them on the web page. So we get what the question was, and the three choices that the user had. We also get whether they voted A, B or C.

We want to say what percentage of the vote a particular choice received. So option A might have received 50 percent of the vote, option B 25 percent, and option C 25 percent. The next few lines of code do just that. Here's the PHP code we have for option A:

$totalP = $qA + $qB + $qC;
$percentA = (($qA * 100) / $totalP);
$percentA = floor($percentA);

The first line gets the total number of votes. We then turn this into a percentage by multiplying the votes cast for A by 100 and then dividing by the grand total of votes. The third line above just uses the inbuilt PHP function floor to round the answer down.

The next few lines get the percentage values for options B and C.

Most of the rest of the code is taken up with building a HTML tag for an image. We want the red bar image to stretch or shrink, depending on what percentage of the vote a choice received. As a reminder, here's what an HTML image tag looks like:

<IMG SRC='red.jpg' HEIGHT=10 WIDTH =100>

If we hard-coded that, we'd get a red image that was 10 pixels high and 100 pixels wide.

But we want to build the HTML image tag with PHP. Here's the first three lines to do that:

$imgWidthA = $percentA * 2;
$imgWidthB = $percentB * 2;
$imgWidthC = $percentC * 2;

What we're doing here is multiplying the individual percentages by 2. If we didn't multiply the image by 2, it would be really small and difficult to see. (You can multiply by a different number, if you want a wider image.) We now have values for the WIDTH part of the HTML tag. The HEIGHT value was something we hard-coded at the top of the page:

$imgHeight = 10;

If you want a taller red line then you can adjust this value.

We now have enough information to build the HTML image tag:

$imgTagA = "<IMG SRC = 'red.jpg' Height = " . $imgHeight . " WIDTH = " . $imgWidthA. ">";

Here, we're using the concatenate operator (.) to join together the various parts of the IMG tag, and placing it into a variable of its own ($imgTagA).

Now examine the HTML in the BODY section of the code:

<body>

<?PHP

print $question . "<BR>";
print $answerA . " " . $imgTagA . " " . $percentA . "% " . $qA . "<BR>";
print $answerB . " " .$imgTagB . " " . $percentB . "% " . $qB . "<BR>";
print $answerC . " " .$imgTagC . " " . $percentC . "% " . $qC . "<BR>";

?>

</body>

First, we print the question out. Then we join together the various parts for each answer:

print $answerA . " " . $imgTagA . " " . $percentA . "% " . $qA . "<BR>";

We're printing the answer first (one of the three choices the user had). Then we have the IMG tag which we built up earlier. Next comes the actual percentage, and finally the actual number of votes cast for that choice. Here is a visual representation of where the various variables are ending up:

PHP variables from the survey page

Not much to do now, for the survey. The final part is adding new questions to the database.