dpjpo746884 2017-04-07 18:05
浏览 29
已采纳

创建循环从表单获取信息字段数量可以变化

I have a form that allows users to register into a database. They can register up to a group of 6 at a time. Currently the webpage displays two forms. The first (userNumberForm) is just a drop down list where the user picks how many names to register. That gets posted and when the page reloads the 2nd form (userInfoForm) runs a while loop to populate the correct number of fields for the user to enter their information in. It also contains a hidden field where the value from the userNumberForm is stored. When the userInfoForm gets posted the first thing I have the addUser.php (the page it posts to) do is check the number of users and store that in a variable called userNum. Currently I have a switch statement based on that variable to get the rest of the information from the form and sanitize the data.

case 2:

    $FirstName1=$_POST['FirstName1'];
    $FirstName2=$_POST['FirstName2'];

    $FirstName1 = stripslashes($FirstName1);
    $FirstName1 = mysql_real_escape_string($FirstName1);
    $FirstName2 = stripslashes($FirstName2);
    $FirstName2 = mysql_real_escape_string($FirstName2);
break;

case 3:

    $FirstName1=$_POST['FirstName1'];
    $FirstName2=$_POST['FirstName2'];
    $FirstName3=$_POST['FirstName3'];

    $FirstName1 = stripslashes($FirstName1);
    $FirstName1 = mysql_real_escape_string($FirstName1);
    $FirstName2 = stripslashes($FirstName2);
    $FirstName2 = mysql_real_escape_string($FirstName2);        
    $FirstName3 = stripslashes($FirstName3);
    $FirstName3 = mysql_real_escape_string($FirstName3);

break;
//... and so on

As you can see this is starting to be a lot of code. I am hoping there is a way to set something up like this:

$x = 1;
while($x < $userNum)
{
$FirstName.$X=$_POST['FirstName.$X'];
$FirstName.$X = stripslashes($FirstName.$X);
$FirstName.$X = mysql_real_escape_string($FirstName.$X);
++$x;
}

Obviously that doesn't work, but that is the format I am looking for. I don't fully understand variable variables yet, Or maybe I'm making this more complicated than it needs to be and there is a way to stick this in an array.

  • 写回答

2条回答 默认 最新

  • doukanhua0752 2017-04-07 19:18
    关注

    Yes a while statement can be used. Remember to append $x outside the string when accessing the variable from $_POST - i.e. $_POST['FirstName' . $x]; (though you could use double-quoted strings, since the variables will be expanded: $_POST["FirstName$x"];). Also the looping condition needs to be updated to less than or equal (i.e. <=) - otherwise the last name will not be processed.

    So update the condition from :

     while($x < $userNum)
    

    To:

    while($x <= $userNum)
    

    Then instead of using $FirstName.$X for the variable, just use a single name - e.g. $FirstName. As long as it is consistent, it will be used for that iteration separately.

    $x = 1;
    while($x <= $userNum)
    {
        $FirstName = $_POST['FirstName' . $x];
        $FirstName = stripslashes($FirstName);
        $FirstName = mysql_real_escape_string($FirstName);
        //use $FirstName to insert into database, or store in temporary structure
        ++$x;
    }
    

    But then also a for statement could also be used. That can reduce the number of lines needed, as the variable to iterate (i.e. $x) is started and incremented in one line.

    for ($x = 1; $x <= $userNum; $x++)
    {
        $FirstName = $_POST['FirstName' . $x];
        $FirstName = stripslashes($FirstName);
        $FirstName = mysql_real_escape_string($FirstName);
        //use $FirstName to insert into database, or store in temporary structure
    }
    

    And taking it one step further, a foreach statement can be used with range(). With this structure, the variable doesn't need to be incremented manually and there is no need for the looping condition (i.e. $x <= $userNum).

    foreach (range(1, $userNum) as $x)
    {
        $FirstName = $_POST['FirstName' . $x];
        $FirstName = stripslashes($FirstName);
        $FirstName = mysql_real_escape_string($FirstName);
        //use $FirstName to insert into database, or store in temporary structure
    }
    

    Or maybe I'm making this more complicated than it needs to be and there is a way to stick this in an array

    Yes you could store those names in an array, like this:

    $FirstNames = array();
    foreach (range(1, $userNum) as $x) {
        $FirstNames[$x] = $_POST['FirstName' . $x];
    }
    //use $FirstNames later to add values to the database 
    

    See a demonstration of this in this phpfiddle.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败