2014-10-08 133 views
0

我的數據庫中的列:stat_id,stat1,stat2,stat3。

我正在使用一種表格,允許我遍歷成員列表並輸入3個不同的統計數據。 ($ member_stat已經是包括「stat_id」作爲其關鍵之一數組)

foreach($member_stats as $member_stat) { 
    echo '<label for="stat1"> Stat 1</label>'; 
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat1" id="stat1" />'; 
    echo '<label for="stat2"> Stat 2</label>'; 
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat2" id="stat2" />'; 
    echo '<label for="stat3"> Stat 3</label>'; 
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat3" id="stat3" /><br />'; 
} 

我點擊提交,我的$ _POST陣列/數據是這樣的:

Array (

    [157stat1] = 1 
    [157stat2] = 4 
    [157stat3] = 7 
    [158stat1] = 2 
    [158stat2] = 2 
    [158stat3] = 6 
    [159stat1] = 8 
    [159stat2] = 6 
    etc... 
) 

我的問題是:我如何把這個$ _POST數組並像上面那樣插入到我的數據庫中?即。

157 stat1 stat2 stat3 
158 stat1 stat2 stat3 
159 stat1 stat2 etc... 

我曾嘗試過各種東西,但我有一個艱難的時間環繞我的頭周圍從不同的統計在$ _ POST鍵分離stat_id(即:157從STAT1,STAT2,STAT3)。我想知道如果我在表單中設置了錯誤,並且應該以其他方式命名我的輸入。任何有識之士將不勝感激。謝謝。

回答

1
$out = array(); 

$post= array(
'157stat1' => 1, 
'157stat2' => 2, 
'157stat3' => 3, 
'158stat1' => 1 
); 

foreach($post as $key => $value) 
{ 
    if (preg_match('/^(\d+)(\w+)$/', $key, $match) !== 0) 
     $out[$match[1]][] = $match[2]; 
} 

var_dump($out); 

之後通過$out循環並準備SQL語句。

foreach($out as $key => $values) 
{ 
    // escape array $values (array('stat1', 'stat2', 'stat3, ...) for each $key) 
    // and string $key (it will be 157, 158 and so on) 
    // prepare and execute SQL statement 
    // function join will help to deal with array $values 
} 
+0

謝謝你的時間。我得到一個錯誤:$ out [$ match [1]] [] = $ match [2];你介意放棄這條線嗎? – user2949794 2014-10-08 23:03:00

+0

@ user2949794你確定你陣列中的所有按鍵看起來都像「157stat1」嗎?我不這麼認爲。好的,我會修改答案來考慮這一點。 – Cheery 2014-10-08 23:10:44

+0

是所有的鍵都是3位數(stat_id),後跟stat1,stat2或stat3。 – user2949794 2014-10-08 23:14:28

0

使用HTML數組。

foreach($member_stats as $member_stat) { 
    echo '<label for="stat1"> Stat 1</label>'; 
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat1]" id="stat1" />'; 
    echo '<label for="stat2"> Stat 2</label>'; 
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat2]" id="stat2" />'; 
    echo '<label for="stat3"> Stat 3</label>'; 
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat3]" id="stat3" /><br />'; 

}

這將返回類似:

陣列(

[157stat][1] = 1 
    [157stat][2] = 4 
    [157stat][3] = 7 
    [158stat][1] = 2 
    [158stat][2] = 2 
    [158stat][3] = 6 
    [159stat][1] = 8 
    [159stat][2] = 6 
) 

你可以看到一個例子:http://www.thefutureoftheweb.com/blog/use-arrays-with-html-form-inputs和另一個偉大的堆棧溢出答案在這裏:HTML input arrays

0

試想一下,你的$ _ POST來這樣的:

array(
    'stats' => array(
     157 => array(
      stat1 => 1, 
      stat2 => 3, 
      stat3 => 4 
     ), 
     158 => array(
      stat1 => 2, 
      stat2 => 3, 
      stat3 => 1 
     ) 
     . 
     . 
    ) 
) 

,那麼你可以只是:

foreach ($_POST['stats'] as $whateverId => $stats) { 
    // now you have the desired $whateverId and 
    // its $stats 
    // you can access each stats indexing $stats like: $stats['stat1'] 
} 

最後,你可以使用下面的HTML表單完成所需的$ _ POST格式命名(使用數組符號):

foreach($member_stats as $member_stat) { 
    $id = $member_stat['stat_id']; 

    echo '<label for="stat1"> Stat 1</label>'; 
    echo "<input type=\"text\" name=\"stats[$id][stat1]\" id=\"stat1\" />"; 

    echo '<label for="stat2"> Stat 2</label>'; 
    echo "<input type=\"text\" name=\"stats[$id][stat2]\" id=\"stat2\" />"; 

    echo '<label for="stat3"> Stat 3</label>'; 
    echo "<input type=\"text\" name=\"stats[$id][stat3]\" id=\"stat3\" /><br />"; 
} 

不要忘記驗證你的$ _POST ['stats']輸入。