Does it have to make the user enter the number of rows and columns of a table and this table appears in PHP?

0

I must create a code that the user can enter the number of rows and columns a table should have, and this table should appear to the user. I try this code here, but the table does not appear with the rows and columns I typed:

    <?php
echo '<form action="Q.php" method="post">Digite o número de linhas: <input type="text" name="linhas"><br>Digite o número de colunas: <input type="text" name="colunas"><br><input type="submit" value="Construir tabela"></form>';
$linhas = $_POST['linhas'];
$colunas = $_POST['colunas'];
$l = 0;
$c = 0;
echo '<table border="1" width="400px" height="300px">';
while($l <= $linhas){
    $l++;
    echo '<tr>';
    while($c <= $colunas){
        $c++;
        echo '<th></th>';
    }
    echo '</tr>';
}


?>

Thanks in advance for your response

    
asked by anonymous 03.05.2017 / 23:44

3 answers

0

As I've commented, you're not restarting the $c variable in every row loop ... so the table is only generated for the first row! Here's an example:

<form method="POST">
<label>Linhas: <input type="text" name="linhas" /></label>
<br />
<label>Colunas: <input type="text" name="colunas" /></label>
<br />
<input type="Submit" value="Gerar tabela" />
</form>

<?php
$linhas = isset($_POST['linhas']) ? $_POST['linhas'] : -1;
$colunas = isset($_POST['colunas']) ? $_POST['colunas'] : -1;
if($linhas > 0 && $colunas > 0)
    echo "Criando uma tabela de " . $linhas . " linhas por " . $colunas . " colunas";

echo "<table border='1' cellpading='10' cellspacing='10' style='border:1px solid #000; width:" . ($colunas * 100) . "px;height:" . ($linhas * 50). "px;'>";
$l = 0;
while($l < $linhas)
{
    $l++;
    echo "<tr>";
    $c = 0;
    while ($c < $colunas)
    {
        $c++;
        echo "<td style='border:1px dashed #F3F;'>L" . $l . "C" . $c . "</td>";     
    }
    echo "</tr>";   
}
echo "</table>";
?>
    
04.05.2017 / 00:10
0

By focusing only on the PHP part, you can use the for loop:

function generate_html_table ($rows, $cols)
{
    // Se linha ou coluna for menor ou igual a zero
    // retorna uma string vazia
    if ($rows <= 0 || $cols <= 0)
    {
        return "";
    }

    // Gera a tabela HTML
    $table = "<table>\n";

    // Percorre as linhas
    for($i = 0; $i < $rows; $i++)
    {
        $table .= "\t<tr>\n";

        // Percorre as colunas
        for ($j = 0; $j < $cols; $j++)
        {
            $table .= "\t\t" . sprintf('<%1$s></%1$s>', ($i == 0) ? "th" : "td") . "\n";
        }

        $table .= "\t</tr>\n";
    }

    // Finaliza a tabela
    $table .= "</table>";

    // Retorna o resultado
    return $table;
}

echo generate_html_table(2, 2);

The result is:

<table>
    <tr>
        <th></th>
        <th></th>
    </tr>
    <tr>
        <td></td>
        <td></td>
    </tr>
</table>

Now, joining with the HTML form:

<form method="POST">
    <label>Linhas: <input type="text" name="rows" /></label>
    <label>Colunas: <input type="text" name="cols" /></label>
    <input type="Submit" value="Gerar tabela" />
</form>

<?php

// Verifica se houve uma requisição POST
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $rows = 0;
    $cols = 0;

    // Valida o valor informado para linhas
    if (!filter_var($_POST["rows"], FILTER_VALIDATE_INT) === false) {
        $rows = $_POST["rows"];
    }

    // Valida o valor informado para colunas
    if (!filter_var($_POST["cols"], FILTER_VALIDATE_INT) === false) {
        $cols = $_POST["cols"];
    }

    echo generate_html_table($rows, $cols);
}

?>
    
04.05.2017 / 00:37
0

example - ideone

$numerolinhas = $_POST["linhas"];
$numerocolunas = $_POST["colunas"]; 
$linhas = "";
$linhas = " <tr>\n";
$controws = 0;
$colunas = "";
$colunas = "  <td></td>\n";
$th = "  <th></th>\n";

echo "<table>\n";   
for ($i = 0; $i < $numerolinhas; $i++) {
    echo $linhas;

        if ($i==0){
            for ($k = 0; $k < $numerocolunas; $k++) {
                echo $th;
            }
            echo " </tr>\n<tr>\n";  
        }


        for ($x = 0; $x < $numerocolunas; $x++) {
            echo $colunas;
        }
    echo " </tr>\n";
    $th="";
}   
echo "</table>";
    
04.05.2017 / 00:46