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:

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:

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:

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:

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:

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:

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