开发者

variable php variables :D

开发者 https://www.devze.com 2023-02-02 17:54 出处:网络
I have the ability to add custom fields in开发者_运维知识库 a project I am making. I have a page that has all the text inputs on it. Each custom field is named consecutively (field1, field2, field3) a

I have the ability to add custom fields in开发者_运维知识库 a project I am making. I have a page that has all the text inputs on it. Each custom field is named consecutively (field1, field2, field3) according to the order they were created. Since the user will have the ability to add as many as they want, how can I select each one so as to post their values to the database?

Hope this makes sense...


You should name the fields with array notation, as follows:

<input name='field[]' type='text' />
<input name='field[]' type='text' />

You can then retrieve the data from $_POST (or $_GET) as

$_POST['field'][0]
$_POST['field'][1]


try this:

for($i=1;$i<=3;$i++) {
  print ${'field' . $i} . "<br>";
}


Here's how you would collect them if they are passed in _GET/_POST

$i = 1;
$custom_fields = array();

while (!empty($_REQUEST["field$i"]) {
    $custom_fields[] = $_REQUEST["field$i"];
    $i++;
}


Assuming that each of these fields will be posted, you can use a simple while loop and check that the variable is set:

$i = 1;
while ( isset($_POST['field' . $i]) ) {
    // Do what you need to do to the variable here    

    // Whatever you do, do not forget this line
    $i += 1;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消