PHP color tables, rows and columns

1

How could I structure the following code adequately to achieve these results? I'm a bit lost with the PHP theme to "merge" with the html. If it is 4x4 it shows 16 numbers, the odd ones in green and the pairs in red.

<html>
    <head>
        <meta charset="UTF-8">
        <title>Dibujando tabla</title>
    </head>
    <body>
       <?php
       if(isset($_POST["enviar"]) && (!empty($_POST["filas"])) && (!empty($_POST["columnas"]))) {
             $filas=$_POST["filas"];
             $columnas=$_POST["columnas"];
             $incremento=1;
             for ($j=1; $j<=$filas; $j++) {
                for ($i=0; $i<=columnas; $i++) {
                    $multiplica=$incremento*$i; 
                    echo $incremento. "x" .$i. "=" .$multiplica. "<br>"; 
                    print("<td>" .$incremento[$filas][$columnas]. "</td>");
                }
                $incremento++;
                echo "<br>";
             }
         }else {

           ?>
        <form name="formulario" action=' <?php echo $_SERVER['PHP_SELF']?>' method="post">
        <div>
        <label for="name">Indica el número de filas: </label>
        <input type="number" name="filas"/> 
        </div>
        <div>
        <label for="name">Indica el número de columnas: </label>
        <input type="number" name="columnas"/> 
        </div> 

        <input type="submit" value="enviar" name="enviar">
        <input type="reset" value="Borrar" name="Borrar">

    </body>
</html>

<?php
    }
?>
    
asked by user09b 26.10.2018 в 14:17
source

2 answers

1

To start you have a syntax error, you need the $ in the for of the $ i (it must be $columnas ), so I see in your code you generate the multiplication tables, there is nothing related to generate even or odd numbers, saying this you have an ex. to paint the even and odd columns adapted to your multiplication table code, make the changes to fit what you really need.

<html>
    <head>
        <meta charset="UTF-8">
        <title>Dibujando tabla</title>
    </head>
    <body>
       <?php
       if(isset($_POST["enviar"]) && (!empty($_POST["filas"])) && (!empty($_POST["columnas"]))) {
             $filas=$_POST["filas"];
             $columnas=$_POST["columnas"];
             $incremento=1;


             for ($j=1; $j<=$filas; $j++) {
                 echo '<table style="float: left; display: inline-block;">';
                        for ($i=0; $i<=$columnas; $i++) {

                            // define color para la columna par o impar la columna
                            $style = (($j % 2) == 0) ? 'color: red;' : 'color: green;';

                            $multiplica=$incremento*$i;
                            echo '<tr>
                                    <td style="'.$style.'">'.$incremento. 'x' .$i. '=' .$multiplica.$incremento[$filas][$columnas]. '</td>'.
                                '</tr>';

                            // otra opción para hacer el echo
                            //echo "
                            //<tr>
                            //    <td style='{$style}'>{$incremento}x{$i}={$multiplica}{$incremento[$filas][$columnas]}</td>
                            //</tr>";

                        }
                    $incremento++;
                 echo '</table>';
             }
         }else {

           ?>
        <form name="formulario" action=' <?php echo $_SERVER['PHP_SELF']?>' method="post">
        <div>
        <label for="name">Indica el número de filas: </label>
        <input type="number" name="filas"/>
        </div>
        <div>
        <label for="name">Indica el número de columnas: </label>
        <input type="number" name="columnas"/>
        </div>

        <input type="submit" value="enviar" name="enviar">
        <input type="reset" value="Borrar" name="Borrar">

    </body>
</html>

<?php
    }
?>
    
answered by 26.10.2018 в 15:08
0

Use the code you indicate and my result was:

<html>
  <head>
    <meta charset="UTF-8">
    <title>Dibujando tabla</title>
  </head>
  <body>

<?php

if((!empty($_POST["filas"])) and (!empty($_POST["columnas"]))) {
  $filas = $_POST["filas"];
  $columnas = $_POST["columnas"];
}else{
  $filas = 4;
  $columnas = 4;
}


$numero = 1;

$tabla = '<table border="1">';

for($i = 1; $i <= $filas; $i++) {

    $tabla .= "<tr>";

    for($j = 1; $j <= $columnas; $j++) {

        if($j % 2 == 0) {
            $tabla .= "<td style='background:red;'>Par ".$numero."</td>";
        }else{
            $tabla .= "<td style='background:green;'>Impar ".$numero."</td>";
        }

        $numero++;

    }

    $tabla .= "</tr>";

}

$tabla .= "</table>";

echo $tabla;

?>

    <form name="formulario" action=' <?php echo $_SERVER['PHP_SELF']?>' method="post">
      <div>
        <label for="name">Indica el número de filas: </label>
        <input type="number" name="filas" min="0"/> 
      </div>
      <div>
        <label for="name">Indica el número de columnas: </label>
        <input type="number" name="columnas" min="0"/> 
      </div> 
      <input type="submit" value="enviar" name="enviar">
      <input type="reset" value="Borrar" name="Borrar">
    </form>
  </body>
</html>

The magic is in the second for where I evaluate if it's inpar or even by dividing it by 2. You can see it working here .

    
answered by 26.10.2018 в 15:31