Sum of several variables in PHP?

1

It happens that I have this code armed in a Dreamwever platform, I need to find a way for all the data generated automatically by the "rand" command to be added in the "Totals" box, for both variables, both for x and for and. but I have no idea how to do it

"<?php
if ( isset($_POST['calcular']) ) {
$numero = $_POST['numero'];
}else{
$numero = 0;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<table width="200" border="1" align="center">
<tr>
  <td colspan="2" align="center" bgcolor="#00FF66">REGRESION LINEAL </td>
</tr>
<tr>
  <td width="50%" bgcolor="#666699"><div align="center">Numero</div></td>
  <td width="50%" align="center" bgcolor="#669966"><input name="numero" 
 type="text" id="numero" value="<?php echo $numero ; ?>" size="10" 
 maxlength="10" /></td>
</tr>
<tr>
  <td colspan="2" align="center" bgcolor="#99FF00"><input name="calcular" type="submit" id="calcular" value="CALCULAR" /></td>
 </tr>
</table>
</form>
<br />
<br />
<?php
if (isset ($_POST['calcular'])){
**$sumax = 1;
$sumay =2 ;
$sumaxy = 3 ;
$sumaxx = 4** ; (aqui puse 1,2,3,4. porque no se como definir la sumatoria total y no sabia que mas poner para que no me diera error el codigo)
$b=(($numero*$sumaxy)-($sumax*$sumay))/(($numero*$sumaxx)-($sumax*$sumax));
$a= ($sumay-($b*$sumax))/$numero ;

?>
<table width="800" border="1" align="center">
 <tr>
<td width="20%" align="center">Numero</td>
<td width="20%" align="center">X</td>
<td width="20%" align="center">Y</td>
<td width="20%" align="center">XY</td>
<td width="20%" align="center">X^2</td>
 </tr>
 <?php
 for($n=1; $n<=$numero; $n++){
 ?>
<tr>
<td width="20%" align="center"><?php echo $n; ?></td>
<td align="center">
<?php
 $x = rand(20,250);
 echo $x ;
?>  </td>
<td width="20%" align="center">
<?php
 $y = rand(20,210);
 echo $y ;
?>  </td>
<td align="center"><?php echo $x * $y ; ?></td>
<td align="center"><?php echo $x * $x ; ?></td>
</tr>
<?php 
}
?>
<tr>
<td align="center">Totales</td>
<td align="center"><?php echo $sumax ; ?></td>
<td align="center"><?php echo $sumay; ?></td>
<td align="center"><?php echo $sumaxy; ?></td>
<td align="center"><?php echo $sumaxx; ?></td>

</table>
<br />
<br />
<table width="400" border="1" align="center">
<tr>
<td width="50%" align="center">A</td>
<td width="50%" align="center"><?php echo $a ; ?></td>
</tr>
<tr>
<td width="50%" align="center">B</td>
<td align="center"><?php echo $b ; ?></td>
 </tr>
</table>
<?php
}
?>

<p>&nbsp;</p>
</body>
</html>
    
asked by Cristian Bernal 12.04.2018 в 05:03
source

1 answer

1

You can save your variables in an array, and at the end add them to the total of each row using array_sum , like this:

<?php
$numero = 20;
?>
<table width="800" border="1" align="center">
    <thead>
        <tr>
            <td width="20%" align="center">Numero</td>
            <td width="20%" align="center">X</td>
            <td width="20%" align="center">Y</td>
            <td width="20%" align="center">XY</td>
            <td width="20%" align="center">X^2</td>
        </tr>
    </thead>
    <tbody>
        <?php
        $arregloDeX = [];
        $arregloDeY = [];
        $arregloDeXY = [];
        $arregloDeXX = [];

        for($n=1; $n <= $numero; $n++):
            // Generamos los números para X y Y y calculamos los demas valores
            $x = rand(20,250);
            $y = rand(20,210);
            $xy = $x * $y;
            $xx = pow($x, 2);

            // Guardamos las variables en arreglos para luego sumarlos
            $arregloDeX[] = $x;
            $arregloDeY[] = $y;
            $arregloDeXY[] = $xy;
            $arregloDeXX[] = $xx;

        ?>
        <tr>
            <?php // Desplegamos los valores generados en cada iteración ?>
            <td><?php echo $n ?></td>
            <td><?php echo $x ?></td>
            <td><?php echo $y ?></td>
            <td><?php echo $xy ?></td>
            <td><?php echo $xx ?></td>
        </tr>
        <?php
        endfor;
        ?>
    </tbody>
    <tfoot>
        <tr>
            <?php // Desplegamos las sumas de todas las columnas ?>
            <td>Totales</td>
            <td><?php echo array_sum($arregloDeX) ?></td>
            <td><?php echo array_sum($arregloDeY) ?></td>
            <td><?php echo array_sum($arregloDeXY) ?></td>
            <td><?php echo array_sum($arregloDeXX) ?></td>
        </tr>
    </tfoot>
</table>

A possible result would be like this (the values will change with each execution):

<table width="800" border="1" align="center">
    <thead>
        <tr>
            <td width="20%" align="center">Numero</td>
            <td width="20%" align="center">X</td>
            <td width="20%" align="center">Y</td>
            <td width="20%" align="center">XY</td>
            <td width="20%" align="center">X^2</td>
        </tr>
    </thead>
    <tbody>
                <tr>
            <td>1</td>
            <td>193</td>
            <td>122</td>
            <td>23546</td>
            <td>37249</td>
        </tr>
                <tr>
            <td>2</td>
            <td>158</td>
            <td>199</td>
            <td>31442</td>
            <td>24964</td>
        </tr>
                <tr>
            <td>3</td>
            <td>227</td>
            <td>154</td>
            <td>34958</td>
            <td>51529</td>
        </tr>
                <tr>
            <td>4</td>
            <td>249</td>
            <td>57</td>
            <td>14193</td>
            <td>62001</td>
        </tr>
                <tr>
            <td>5</td>
            <td>157</td>
            <td>169</td>
            <td>26533</td>
            <td>24649</td>
        </tr>
                <tr>
            <td>6</td>
            <td>157</td>
            <td>173</td>
            <td>27161</td>
            <td>24649</td>
        </tr>
                <tr>
            <td>7</td>
            <td>41</td>
            <td>154</td>
            <td>6314</td>
            <td>1681</td>
        </tr>
                <tr>
            <td>8</td>
            <td>111</td>
            <td>29</td>
            <td>3219</td>
            <td>12321</td>
        </tr>
                <tr>
            <td>9</td>
            <td>63</td>
            <td>62</td>
            <td>3906</td>
            <td>3969</td>
        </tr>
                <tr>
            <td>10</td>
            <td>238</td>
            <td>155</td>
            <td>36890</td>
            <td>56644</td>
        </tr>
                <tr>
            <td>11</td>
            <td>196</td>
            <td>100</td>
            <td>19600</td>
            <td>38416</td>
        </tr>
                <tr>
            <td>12</td>
            <td>34</td>
            <td>71</td>
            <td>2414</td>
            <td>1156</td>
        </tr>
                <tr>
            <td>13</td>
            <td>132</td>
            <td>49</td>
            <td>6468</td>
            <td>17424</td>
        </tr>
                <tr>
            <td>14</td>
            <td>81</td>
            <td>98</td>
            <td>7938</td>
            <td>6561</td>
        </tr>
                <tr>
            <td>15</td>
            <td>60</td>
            <td>48</td>
            <td>2880</td>
            <td>3600</td>
        </tr>
                <tr>
            <td>16</td>
            <td>174</td>
            <td>196</td>
            <td>34104</td>
            <td>30276</td>
        </tr>
                <tr>
            <td>17</td>
            <td>178</td>
            <td>70</td>
            <td>12460</td>
            <td>31684</td>
        </tr>
                <tr>
            <td>18</td>
            <td>219</td>
            <td>132</td>
            <td>28908</td>
            <td>47961</td>
        </tr>
                <tr>
            <td>19</td>
            <td>244</td>
            <td>183</td>
            <td>44652</td>
            <td>59536</td>
        </tr>
                <tr>
            <td>20</td>
            <td>200</td>
            <td>127</td>
            <td>25400</td>
            <td>40000</td>
        </tr>
            </tbody>
    <tfoot>
        <tr>
                        <td>Totales</td>
            <td>3112</td>
            <td>2348</td>
            <td>392986</td>
            <td>576270</td>
        </tr>
    </tfoot>
</table>
    
answered by 12.04.2018 в 06:01