Output percent values to a webpage

I made a little school survey. I get the values from MySQL and read the values into a php array called numnames

I can successfully display the numbers in a table in html. Below that, I want another table with the percent values.

Each question in the survey has 7 possible answers, I just want to show how many people chose what answer.

But in the percent table, I just get zeros where there should be a percent value. Any tips please??

data[8] is the total number of participants in the survey, data[0] is the question number, like Q1, Q2. These both display OK.

<?php foreach ($numname as $data): ?>
<p>
<tr>
<td> <?php echo htmlspecialchars($data[0], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[1]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[2]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[3]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[4]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[5]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[6]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[7]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[8], ENT_QUOTES, 'UTF-8'); ?> </td>
<tr> 
</p>
<?php endforeach; ?>

If you are creating a FOREACH for an array of items such as Q1, Q2, it is a string! It is NOT an index.
What is inside $numname?
Is it a list such as $numname=“Q1,Q2”; ? Or an array like $numname=array(“Q1”, “Q2”); ?

Your code shows $numname as an array which you loop thru and then you attempt to use parts of a data array with very odd names. Some are strings " $data[] " and some are functions " data[] "… This makes no sense!

Thanks for your reply, let me elaborate: there are 14 questions in this little survey.

PHP reads the answers from a table and increases the count value for each possible answer in a table called surveyU6_3_totals.

If PHP finds answer A for Q1, it increases the value in row Q1, column A1_total by +=1

That way, I get the totals of each reply for all participants in the survey and all possible answers.

surveyU6_3_totals has the columns:

id, Qnr, A1_total, A2_total, A3_total, A4_total, A5_total, A6_total, A7_total

and it has rows Q1 to Q14, for the 14 questions.

So I collect the data with:

$numname[ ] = array(
		$row['Qnr'],
		$row['A1_total'],
		$row['A2_total'],
		$row['A3_total'],
		$row['A4_total'],
		$row['A5_total'],
		$row['A6_total'],
		$row['A7_total'],
		$count
		);

$count is the total number of participants in the survey, a few hundred, taken from the table where the results are stored.

'SELECT COUNT(*) FROM surveyU6_3_data'

This displays the total number of answers for each question and possible answer correctly:

<?php foreach ($numname as $data): ?>
<p>
<tr>
<td> <?php echo htmlspecialchars($data[0], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[1], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[2], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[3], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[4], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[5], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[6], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[7], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[8], ENT_QUOTES, 'UTF-8'); ?> </td>
<tr> 
</p>
<?php endforeach; ?>

data[0] is the question number, like Q1, data[8] is $count.

Anway, I fixed it now: I made:

$numnameP[] = array(
		$row['Qnr'],
		$row['A1_total']/$count*100,
		$row['A2_total']/$count*100,
		$row['A3_total']/$count*100,
		$row['A4_total']/$count*100,
		$row['A5_total']/$count*100,
		$row['A6_total']/$count*100,
		$row['A7_total']/$count*100,
		$count
		);

and then:

<?php foreach ($numnameP as $data): ?>
<p>
<tr>
<td> <?php echo htmlspecialchars($data[0], ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[1]), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[2]), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[3]), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[4]), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[5]), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[6]), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars(intval($data[7]), ENT_QUOTES, 'UTF-8'); ?> </td>
<td> <?php echo htmlspecialchars($data[8], ENT_QUOTES, 'UTF-8'); ?> </td>
<tr> 
</p>
<?php endforeach; ?>

I am not very proficient with this stuff, so I am not sure why the percent values did not show up!

Works now!!

My guess is because you have $data[4] divided by nothing. You are missing a $ before data[8]…
Once you changed the division to be in the array, you fixed it by using $count…

1 Like

Thanks for taking the time!

You are very welcome. See you in your next post!

Sponsor our Newsletter | Privacy Policy | Terms of Service