How to do a little Christmas in PHP?

0

I have this code that this pine generates me

But I want it to come out like this:

<html>
<body>

<h2>Pinos Examen</h2>

<form action="examenpinos.php" method="post">
  Cantidad de nivel:<br>
  <input type="number" name="nivel" value="nivel">
  <br>
  <input type="submit" value="Submit">
</form> 

<?php
$nivel = $_REQUEST['nivel'];

echo "<center>";
    //Condicion que limita el numero de niveles
    if ($nivel >=1 && $nivel<=9){

for ($k=1; $k<=3; $k++)
{
    for ($j=1; $j<=$nivel; $j++)
    {
        for ($i=1; $i<=$j; $i++)
        {
            echo "*";
        }
        echo "<br>";
        $j++;
    }
    $nivel=$nivel+2;
}
    //Imprime el tronco
        echo "***<br>";
        echo "***";
        echo "</center>";
}



?>

</body>
</html>
    
asked by Enmanuel Castle 18.12.2018 в 23:37
source

1 answer

0

This code has been tested and it works.

I have stored the value of the variable $nivel in a constant, to compare where the following triangles would start and thus omit the line break and increase the variable $j .

<html>
<body>

<h2>Pinos Examen</h2>

<form action="examenpinos.php" method="post">
  Cantidad de nivel:<br>
  <input type="number" name="nivel" value="nivel">
  <br>
  <input type="submit" value="Submit">
</form> 
<?php
  $nivel = $_REQUEST['nivel'];
  define('NIVEL', $_REQUEST['nivel']); //constante para almacenar nivel, sin cambios de valor

  echo "<center>";
  //Condicion que limita el numero de niveles
  if ($nivel >=1 && $nivel<=9){

   for ($k=1; $k<=3; $k++){
     for ($j=1; $j<=$nivel; $j++){
        for ($i=1; $i<=$j; $i++){
            echo "*";
        }
        //según esta orden es el primer asterisco de los 2 ultimos triangulos, a los que no se les hará un salto de línea
        if(constant("NIVEL") != $nivel && $j==1){ 
         }else{
           echo "<br>";
           $j++;
         }
      }
      $nivel=$nivel+2;
    }
  //Imprime el tronco
  echo "***<br>";
  echo "***";
  echo "</center>";
}

?>
</body>
</html>
    
answered by 19.12.2018 в 00:36