# Output percent values to a webpage

**URL:** <https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482>\
**Category:** General PHP Help\
**Created:** [May 11, 2021, 9:12am UTC](https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482 "2021-05-11T09:12:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedroski55](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@Pedroski55](https://www.phphelp.com/u/Pedroski55)\
**Post date:** [May 11, 2021, 9:12am UTC](https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482/1 "2021-05-11T09:12:36Z")

</div>

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; ?>
```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [May 12, 2021, 6:42pm UTC](https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482/2 "2021-05-12T18:42:53Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Pedroski55](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@Pedroski55](https://www.phphelp.com/u/Pedroski55)\
**Post date:** [May 13, 2021, 9:35am UTC](https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482/3 "2021-05-13T09:35:10Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [May 13, 2021, 2:35pm UTC](https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482/4 "2021-05-13T14:35:26Z")

</div>

> [@Pedroski55](#):
>
> ```auto
> <td> <?php echo htmlspecialchars(intval($data[4]/data[8]*100), ENT_QUOTES, 'UTF-8'); ?> </td>
> 
> ```

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…

---

<div class="post-metadata">

**Author:** ![Pedroski55](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@Pedroski55](https://www.phphelp.com/u/Pedroski55)\
**Post date:** [May 14, 2021, 12:49am UTC](https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482/5 "2021-05-14T00:49:19Z")

</div>

Thanks for taking the time!

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [May 14, 2021, 1:00am UTC](https://www.phphelp.com/t/output-percent-values-to-a-webpage/33482/6 "2021-05-14T01:00:40Z")

</div>

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