2015-11-27 34 views
-1

我想将用户输入插入到一个php表中。例如,如果我从前一页定义一个2x3表(2行和3列),输入屏幕将显示6个输入,供您填写任何您想要的内容。然后将6个内容变量插入到表中。我相信一个数组和可能是一个foreach循环可能是必要的。但是,我不确定如何让用户输入内容继续到下一页并正确插入到表格中。我也试图把它列入一个列表,但我假设如果表问题可以解决,它应该是相同的列表。任何帮助将不胜感激。PHP动态用户输入到php表

//页,其中定义输入的显示

$row = (isset($_POST['rows']) ? $_POST['rows'] : null); 
$col = (isset($_POST['columns']) ? $_POST['columns'] : null); 
$list = (isset($_POST['listitems']) ? $_POST['listitems'] : null); 
$list2 = (isset($_POST['listitems2']) ? $_POST['listitems2'] : null); 

array('[content[]');//Not sure if array should be here or on next page 

$y = $list; 
$z = $list2; 
$x = $row * $col; 

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

      echo "$i<input type='text' name='content[]' required> <br>"; 

     } 
     for ($t = 1; $t <= $y; $t++) { 

      echo "$t<input type='text' name='content[]' required> <br>"; 
     } 

     for ($s = 1; $s <= $z; $s++) { 

      echo "$s<input type='text' name='content[]' required> <br>"; 
     } 

     echo "<input type='submit' name='submit2' value='Submit'/> 
      <input type='hidden' name='method' value='post' /> 
      <input type='hidden' name='unorderedinput' value='$list' /> 
      <input type='hidden' name='orderedinput' value='$list2' /> 
      <input type='hidden' name='rowsinput' value='$row' /> 
      <input type='hidden' name='columnsinput' value='$col' /> 
      <input type='hidden' name='cont' value='content[]' />"; 

echo "</form>"; 

//页面在显示表中。

 $trow = $_POST['rowsinput']; 
     $tcol = $_POST['columnsinput']; 
     $ulist = $_POST['unorderedinput']; 
     $olist = $_POST['orderedinput']; 
     $tcontent = $_POST['cont']; 

     echo "<table border='1'>"; 

     for ($tr = 1; $tr <= $trow; $tr++) { 
      echo "<tr>"; 
       for ($td = 1; $td <= $tcol; $td++) { 
        echo "<td align='center'>". "$tcontent". "</td>";//I'm not sure if this is correct 
       } 

        echo "</tr>"; 
      } 

     echo "</table>"; 

回答

1

首先,你不不需要这一行:

array('[content[]'); 

它没有做任何事情,因为它没有分配给变量。如果目的是制作内容数组,那么php将在第二页上为您做。见下文。

而且,你并不需要这一行之一:

<input type='hidden' name='cont' value='content[]' />"; 

那是因为你已经有input标签与名称content[],所以直接在接下来的页面,在这里你有这条线使用这些:

$tcontent = $_POST['cont']; 

该行只是制作$tcontent = "content[]",即文字字符串,而不是内容。

而是放:

$tcontent = $_POST['content']; 

...现在你将有值的数组。

随后,同样在第二页上,你可以使用currentnext功能遍历这些值:

echo "<table border='1'>"; 

    $value = current($tcontent); 
    for ($tr = 1; $tr <= $trow; $tr++) { 
     echo "<tr>"; 
     for ($td = 1; $td <= $tcol; $td++) { 
      echo "<td align='center'>". $value . "</td>"; 
      // get next element 
      $value = next($tcontent); 
     } 
     echo "</tr>"; 
    } 

    echo "</table>"; 

你不应该忘记让你的代码处理失踪论据$_POST,使用isset功能,就像你为第一页所做的一样。

+0

它工作完美。谢谢。 – mastodon

+0

不客气! – trincot

0

$_POST['cont']将值在您的示例阵列,所以你首先要检查它是否not empty(),然后用foreach重复一遍,例如:

echo "<table border='1'>"; 

    for ($tr = 1; $tr <= $trow; $tr++) { 
     echo "<tr>"; 
      for ($td = 1; $td <= $tcol; $td++) { 
       if (!empty($tcontent)) { 
        foreach ($tcontent as $singleValue) { 
         echo "<td align='center'>". "$singleValue". "</td>"; 
        } 
       } 
      } 

       echo "</tr>"; 
     } 

    echo "</table>"; 
+0

请注意,即使'$ tcontent'是一个数组(它不是),您仍然会在'for'循环的每次迭代中生成完整的值列表。 – trincot