I'm starting with PHP and I have to do an exercise to show calculated results from an array in a table. I get the data but at the time of creating the table I do not give the formula to do it horizontally. I mean, I get to do it like this:
But I need to do it like this:
That is, vertically. After many laps I do not give with the solution. I put the code that I used:
Frequencies
#verde{
background: green;
color: white;
text-align: center;
}
table{
border-collapse:collapse;
background-color: #97BB00;
border-color: white;
text-align: center;
}
</style>
</head>
<body>
<table border="solid" align="center" >
<?php
//Vavriables
$valores = array(7, 19, 25, 12, 23, 15, 8, 16);
$total = array_sum($valores);
echo "<tr><td id=\"verde\"><b>X1</b></td>";
for ($i = 0; $i < count($valores); $i++) {
$indice = $i + 1;
echo "<td> <b>$indice</b> </td>";
}
echo "<td id=\"verde\"><b>TOTAL<b></td>";
echo "</tr>";
echo "<tr><td id=\"verde\"><b>Frecuencia<br> absoluta</b></td>";
foreach ($valores as $value) {
echo "<td>$value</td>";
}
echo "<td id=\"verde\"><b>$total</b></td>";
echo "</tr>";
echo "<tr><td id=\"verde\"><b>Frecuencia<br> absoluta <br>acumulada</b></td>";
$frecuenciaAbAcumulada = 0;
foreach ($valores as $value) {
$frecuenciaAbAcumulada += $value;
echo "<td>$frecuenciaAbAcumulada</td>";
}
echo "<td id=\"verde\"><b>$total</b></td>";
echo "</tr>";
echo "<tr><td id=\"verde\"><b>Frecuencia<br>relativa</b></td>";
$fr = 0;
$frecuenciaRelativa = 0;
foreach ($valores as $value) {
$fr = $value / array_sum($valores);
$frecuenciaRelativa = round($fr, 2, PHP_ROUND_HALF_UP);
echo "<td>$frecuenciaRelativa</td>";
}
$totalFrecuenciaRelativa = 0;
foreach ($valores as $value) {
$totalFrecuenciaRelativa += ($value / array_sum($valores));
}
echo "<td id=\"verde\"><b>$totalFrecuenciaRelativa</b></td>";
echo "</tr>";
echo "<tr><td id=\"verde\"><b>Frecuencia<br>Relativa<br>acumulada</b></td>";
$fra = 0;
$frecuenciaRelativaAcumulada = 0;
foreach ($valores as $value) {
$frecuenciaRelativaAcumulada += $value / array_sum($valores);
$fra = round($frecuenciaRelativaAcumulada, 2, PHP_ROUND_HALF_UP);
echo "<td>$fra</td>";
}
echo "<td id=\"verde\"><b>$fra</b></td>";
echo "</tr>";
?>
</table>
</body>
Greetings.