2012-12-17 2 views
0

많은 삽입 필드가있는 페이지에서 작업하고 있습니다. 다음 코드는 어떻게 단축 할 수 있습니까?

$title_1 = $_POST['title_1']; 
$content_1 = $_POST['content_1']; 
$link_1 = $_POST['link_1']; 
$img_link_1 = $_POST['img_link_1']; 

$title_2 = $_POST['title_2']; 
$content_2 = $_POST['content_2']; 
$link_2 = $_POST['link_2']; 
$img_link_2 = $_POST['img_link_2']; 

$title_3 = $_POST['title_3']; 
$content_3 = $_POST['content_3']; 
$link_3 = $_POST['link_3']; 
$img_link_3 = $_POST['img_link_3']; 
+1

사용 배열. –

+0

'if ($ _POST [ 'title_1'] == ...) {...}' –

답변

1

이 같은 $ _POST 배열을 루프 수 :

foreach ($_POST as $key => $value) { 
    ${$key} = $value; 
} 

이 같은 게시물 변수를 만들 것입니다 $_POST['title_1'] ~ $title_1

귀하의 게시물 이름은 변수가 참조하고자하는 정확한 이름이어야합니다.

+0

브릴리언트. 나는 $ {$ variable} 보간을 알지 못했지만, 가능하다면 항상 궁금해했습니다. –

0

게시물을 편집 한 후에이 대답을 다시 입력했습니다. 변수 변수를 사용하십시오.

foreach ($_POST as $key => $val) 
{ 
    if (preg_match('/^(([a-z]+_)+\d)$/', $key, $match) 
    { 
     $$match[0] = $val; 
    } 
} 

사용 [a-z0-9] 또는 대안으로 [a-zA-Z0-9].

1

내가 할 것이다 :

$howmany = 3; // How many sets of fields are submitted. 

for($i=0;$i<$howmany;$i++){ 
    $field[$i]['title'] = $_POST['title_'.$i]; 
    $field[$i]['content'] = $_POST['content_'.$i]; 
    $field[$i]['link'] = $_POST['link_'.$i]; 
    $field[$i]['img_link'] = $_POST['img_link_'.$i]; 
} 

은 그럼 당신은 $field[1]['title'] 형태로 데이터에 액세스 할 수 있습니다.

0

사용할 수 추출물 (http://php.net/manual/en/function.extract.php) : extract($_POST)

하지만 당신은 조심해야한다 - 클라이언트 게시물 어떤 USER_ID, 또는 어떤 경우? 적어도, 당신은 $ _POST 값이 이미 정의 변수를 덮어 쓰지 않도록 지정해야합니다 extract($_POST, EXTR_SKIP)

0
<?php 
$key_prefixes = array (
    'title', 
    'content', 
    'link', 
    'img_link' 
); 

$i = 1; 
while (true) { 
    $post_values_missing = 0; 
    foreach ($key_prefixes as $key_prefix) { 
     $key = $key_prefix . '_' . $i; 
     if (!isset($_POST[$key])) { 
      $post_values_missing += 1; 
      continue; 
     }; 
     $val = $_POST[$key]; 
     // do something with $val 
    } 

    // did you get any values through this iteration? 
    $post_values_exist_bool = (count($key_prefixes) !== $post_values_missing); 

    // if not, you must've gotten them all 
    if (false === $post_values_exist_bool) { 
     break; 
    } 

    $i += 1; 
} 
0

가장 확실한 방법은 PHP의 POST 데이터 처리 기능을 사용하여 작업을 수행하는 것입니다.

는 다음과 같이 HTML 양식 이름을 사용하는 것이 좋습니다 다음과 같이 PHP에서

<form action="{url}" method="post"> 

    <input type="text" name="data[0][title]" /> 
    <input type="text" name="data[0][content]" /> 
    <input type="text" name="data[0][link]" /> 
    <input type="text" name="data[0][image_link]" /> 

    <input type="text" name="data[1][title]" /> 
    <input type="text" name="data[1][content]" /> 
    <input type="text" name="data[1][link]" /> 
    <input type="text" name="data[1][image_link]" /> 

    ... 

</form> 

이 데이터를 추출 :

$data = $_POST['data']; 

이것은 단지 하나 개의 라인에 PHP 코드를 단축. 이 문장은 data 양식 입력의 PHP 배열을 직접 제공합니다. var_dump은 다음과 같이 표시됩니다.

array (
    0 => array('title'=>'...','content'=>'...','link'=>'...','image_link'=>'...'), 
    1 => array('title'=>'...','content'=>'...','link'=>'...','image_link'=>'...'), 
    ... 
) 
+0

꽤 인상적입니다. 나는 그것을 몰랐다. 팁 고마워! –

+0

@ChristopheMarois 여러분을 환영합니다! :) –

0

이름을 변경할 필요가 없습니다. 단지 배열로 지정하십시오.

<input type="text" name="title[]" /> 
<input type="text" name="content[]" /> 
<input type="text" name="link[]" /> 
<input type="text" name="image_link[]" /> 

<input type="text" name="title[]" /> 
<input type="text" name="content[]" /> 
<input type="text" name="link[]" /> 
<input type="text" name="image_link[]" /> 

<input type="text" name="title[]" /> 
<input type="text" name="content[]" /> 
<input type="text" name="link[]" /> 
<input type="text" name="image_link[]" /> 

PHP : 필드와

extract($_POST); 
$count=count($title); 

for($i=0;$i<$count;$i++) { 

//You can perform your any function on this loop, to get title use $title[$i] 
}