Código de ejemplo que genera una tabla de acuerdo al input del usuario. Permite agregar filas y columas.

PHP:
  1. <?php
  2. /*
  3. *  Código de Ejemplo
  4. *  César Sánchez Oropeza jesama@prodigy.net.mx
  5. *  www.novacreations.net
  6. *
  7. * Este código es una página que genera una matriz de forma dinámica desplegandola como una tabla,
  8. * Permite agregar filas y columnas, y modificar los datos de la misma.
  9. *
  10. */
  11.  
  12. echo "<html>";
  13. echo "<body>";
  14.  
  15. // PRIMER ACCESO A LA PAGINA
  16. if(!$_POST)
  17. {
  18.     echo "<p>ingresa la cantidad de filas y columnas que deseas para tu tabla</p>";
  19.     echo "<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">";
  20.     echo"FILAS: <input name=\"filas\" type=\"text\">";
  21.     echo"COLUMNAS: <input name=\"columnas\" type=\"text\">";
  22.     echo "<input name=\"boton\" type=\"SUBMIT\" value=\"CREAR\">";
  23.     echo "</form>";
  24.  
  25.  
  26. }
  27. else if(isset($_POST['boton']) && $_POST['boton']=="Enviar") // Boton de enviar, imprime el arreglo
  28. {
  29.     unset($_POST['columnas']); // quitamos los botones del arreglo
  30.     unset($_POST['filas']); // quitamos los botones del arreglo
  31.     unset($_POST['boton']);
  32.     echo "<pre>";
  33.     print_r($_POST); // imprimimos la matriz, el arreglo es unidimensional
  34.     echo "</pre>";
  35. }
  36. else
  37. {
  38.  
  39.  
  40.     echo "<form method=\"POST\" action=\"".$_SERVER['PHP_SELF']."\">";
  41.    
  42.     // creamos la fila o culumna en el arreglo
  43.     if($_POST['boton']=="Agregar Fila") $_POST['filas']++;
  44.     if($_POST['boton']=="Agregar Columna") $_POST['columnas']++;
  45.  
  46.     // almacenamos el valor de las filas y columnas
  47.     echo "<input name=\"filas\" type=\"hidden\" value=\"".$_POST['filas']."\">";
  48.     echo "<input name=\"columnas\" type=\"hidden\" value=\"".$_POST['columnas']."\">";
  49.    
  50.     //imprimimos los datos de la tabla
  51.     echo "<table border=\"1\">";
  52.     echo "<tr><td></td>";
  53.     for($i = 0; $i < $_POST['columnas'] ; $i++) echo "<td>".$i."</td>"; // imprimimos el numero de las columnas
  54.     echo "</tr>";   
  55.     for($i = 0; $i < $_POST['filas'] ; $i++) //filas
  56.     {
  57.         echo "<tr>";
  58.         echo "<td>".($i+1)."</td>"; // imprime el numero de la fila
  59.         for($j = 0; $j <$_POST['columnas']$j++) //columnas
  60.         {
  61.             $art = ""; //inicalizamos la variable
  62.             if(isset($_POST[$i."-".$j]))$art = $_POST[$i."-".$j]; // imprimimos el valor del arreglo
  63.             echo "<td><input name=\"".$i."-".$j."\" type=\"text\" value=".$art."></td>"; //
  64.     }
  65.  
  66.     echo "</tr>";      
  67. }
  68.    
  69.  
  70. echo "</table>";
  71. echo "<input name=\"boton\" type=\"SUBMIT\" value=\"Agregar Fila\">";
  72. echo "<input name=\"boton\" type=\"SUBMIT\" value=\"Agregar Columna\">";
  73. echo "<input name=\"boton\" type=\"SUBMIT\" value=\"Enviar\">";
  74. echo "</form>";
  75. }
  76.  
  77.  
  78. echo "</body>";
  79. echo "</html>";
  80.  
  81.  
  82.  
  83. ?>

Popularidad: 7%