2012-07-30 72 views
6

我有一個問題,你如何處理從PHP和MySQL的形式的巨大數據?如何處理巨大的表格

我有下: 〜50場(輸入文本,checkboxex,選擇文本域)

在那之後,我需要把這個保存到MySQL數據庫,我需要選擇和過濾此數據。 您有練習嗎,您在項目中使用了什麼?

+0

是否有一個具體的問題?沒有本地抑制器阻止您提交50個字段(當然,通過POST - 使用GET最終可能會遇到URL長度限制)。 – Utkanos 2012-07-30 11:53:01

+0

[here you go](http://stackoverflow.com/questions/1411811/how-to-post-a-form-with-many-fields-with-jquery)假設你的'huge'等於'many fields'而不是'大字段' – 2012-07-30 11:56:13

+0

我的意思是也許有框架或方法,可以幫助我。我非常厭倦通過$ _POST ['field'] - >插入數據庫來處理。 – d7p4x 2012-07-30 11:59:39

回答

6

要以這種形式組織數據,可以使用HTML表單數組。假設我們提交了大量有關房屋的數據。我會將數據分成幾個部分,例如:general,geo,features,descriptions並組成這樣的形式。

<form> 
 
    <fieldset> 
 
    <legend>General information</legend> 
 
    <input type="number" name="general[pax]" value="" placeholder="Pax" /> 
 
    <input type="number" name="general[pets]" value="" placeholder="Pets" /> 
 
    <input type="text" name="general[type]" value="" placeholder="Type" /> 
 
    </fieldset> 
 

 
    <fieldset> 
 
    <legend>GEO data</legend> 
 
    <input type="text" name="geo[longitude]" value="" placeholder="Longitude" /> 
 
    <input type="text" name="geo[latitude]" value="" placeholder="Latitude" /> 
 
    </fieldset> 
 

 
    <fieldset> 
 
    <legend>Features</legend> 
 
    <input type="checkbox" name="features[internet]" value="1" title="Internet" /> 
 
    <input type="checkbox" name="features[pool]" value="1" title="Pool" /> 
 
    <input type="checkbox" name="features[conditioner]" value="1" title="A/C" /> 
 
    </fieldset> 
 
</form>

UPDATE:使用<fieldset><legend>標籤和一點jQuery沒有表現出),你可以很容易地顯示/隱藏不同的組,並在你的品味他們的名字

提交這樣的表格後,您將能夠訪問像值:

$pets = (int)$_POST['general']['pets']; 
$features = $_POST['features']; 

$lon = (float)$_POST['geo']['longitude']; 
$lat = (float)$_POST['geo']['latitude']; 

這將簡化開發,降低努力控制/解析/枚舉數據的不同羣體。

UPDATE:或一個以上可能的變化是

<input type="text" name="params[param1]" value="" /> 
<input type="text" name="params[param2]" value="" /> 
<input type="text" name="params[param3]" value="" /> 

同時在PHP

$params = $_POST['params']; // getting our isolated array of parameters 
$names = array_keys($params); // extracting names from array 
$values = array_values($params); // extracting values from array 

$mysql->insert($names, $values) // and trying to implement desired solution 
+0

是的,我也喜歡它,但是當我打開我的PHP腳本並寫入smth如:$ varA = $ _POST ['param1] .... $ varZ = $ _POST ['param1000']我鬱悶。 – d7p4x 2012-07-30 12:24:13

+0

@ d7p4x,我已用'

'和''標籤更新了我的答案,以便能夠在表單中組織數據組。 – 2012-07-30 12:34:06

+1

我不認爲

是必需的,我只使用name =「program [1]」.. name =「program [10]」,它的工作原理=),但它不舒服。我想要smth:$ params [] [] = $ _POST ['params']; $ mysql-> insert($ params ['names'],$ params ['values'])是否是reall? =) – d7p4x 2012-07-30 12:53:07

1

我會將信息分成幾個部分,每個部分只顯示/詢問一個子集。每個部分都有一個保存/下一個按鈕,數據保存在每個部分提交。