Do you have to make the user type the number of rows and columns of a table and this table appears in PHP?

Asked

Viewed 842 times

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>';
}


?>

Thank you for your reply

  • Your $c counter is not being restarted in the second row, so it just creates the first row columns... See my answer below with correction

  • What is the result of your code? Did you look at the source code to make sure that the table was not generated? By the way, to get the values in $_POST you should make sure that a POST request has been made for the file, what’s more, you need to restart the column counter to each row and end with the tag </table>. The element th sets the table header, so it is expected that only the first row exists. In the others, use the element td.

3 answers

1

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

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);
}

?>

0

As I commented, you are not restarting the variable $c in each loop of the rows...so the table is only generated for the first row! Follows example working:

<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>";
?>

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>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.