2012-12-17 61 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)

但你要小心 - 如果客戶端的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表單名稱如下:

<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> 

在PHP中提取數據如下:

$data = $_POST['data']; 

這將縮短你的PHP代碼只是一條線。這條語句將直接給你一個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] 
}