2014-01-16 104 views
-2

我有一個表單提交了大量需要循環並執行操作的小字段。PHP通過表單值循環

的形式如下:

<input class="" type="hidden" name="pid" value="10"> 
<input class="" type="hidden" name="date" value="01-01-2014"> 

然後約100 - 200的這些...

<input class="minnight" type="text" name="minnight_12_2014-03-06" value="2" size="1"> 
<input class="minnight" type="text" name="minnight_12_2014-03-07" value="2" size="1"> 
<input class="minnight" type="text" name="minnight_12_2014-03-08" value="1" size="1"> 
<input class="minnight" type="text" name="minnight_13_2014-03-06" value="3" size="1"> 
<input class="minnight" type="text" name="minnight_13_2014-03-07" value="2" size="1"> 
<input class="minnight" type="text" name="minnight_13_2014-03-08" value="4" size="1"> 
<input class="minnight" type="text" name="minnight_14_2014-03-06" value="1" size="1"> 
<input class="minnight" type="text" name="minnight_14_2014-03-07" value="2" size="1"> 
<input class="minnight" type="text" name="minnight_14_2014-03-08" value="2" size="1"> 

當提交表單時,我通過每個 「minnight」 字段需要循環,將「12_2014-03-06」提取爲「12」和「2014-03-06」,將它們存儲在變量中,最後在進入下一個之前執行數據庫查詢。

任何指向我應該從這混亂開始的地方?

+1

是。從自己嘗試一些事情開始,然後發佈代碼。 – Leonardo

+0

看看foreach,http://br2.php.net/manual/en/control-structures.foreach.php ...然後從密鑰中提取你需要的數據 – Lefsler

+0

你到目前爲止嘗試過哪些代碼? Stackoverflow不是讓社區告訴你如何編寫代碼,而是教你如何編寫代碼。 – Enijar

回答

5

看進用explode()POST variables

試試沿着線:

// Loop over each item in the form. 
foreach($_POST as $name => $value) { 
    // Split the name into an array on each underscore. 
    $splitString = explode("_", $name); 

    // If the data begins with "minnight", use it. 
    if ($splitString[0] == "minnight") { 
     // Set the other desired values into variables. 
     $secondValue = $splitString[1]; 
     $thirdValue= $splitString[2]; 

     // Database query goes here. 
    } 
} 
+0

看起來很完美。我剛想到一個令人討厭的preg_match循環,但你的看起來更清潔! – mattltm