2013-07-10 21 views
4

這使我很困惑, 我無法將我的表格保存到我的數據庫中。foreach保存到mysqli

請看下圖。 形式圖像 enter image description here

數據庫的例子不僅節省...

example db

所以當你看到它不是1,2,3,4但在圖像上是1,3,2 ,4 我該如何有條不紊地保存它?

HTML代碼

<table cellpadding="5" cellspacing="0" width="100%" class="computation_table"> 
<tr> 
    <th></th> 
    <th>Categorie&euml;n</th> 
    <th>Aantal</th> 
    <th>Omschrijving</th> 
    <th>Bedrag</th> 
    <th>Totaal</th> 
    <th>BTW</th> 
    <th></th> 
</tr> 
<tr> 
    <td> 
    <a id="add_new_element"><img src="images/add.png" width="20" alt="add product" /></a> 
    <a id="add_new_category"><img src="images/cat.png" alt="add category" /></a> 
    </td> 
    <td align="left" colspan="6"> 
     <input type="text" name="input[0][cat_name]" style="font-weight:bold;" value="Categorie naam" /> 
    </td> 
</tr> 
<tr> 
    <td> 
    <a id="add_new_element"><img src="images/add.png" width="20" alt="add product" /></a> 
    <a id="add_new_category"><img src="images/cat.png" alt="add category" /></a> 
    </td> 
    <td> 
     <input type="text" name="input[0][category]" class="text_com" value="" /> 
    </td> 
    <td> 
     <input type="text" name="input[0][quantity]" id="new_quan" class="quantity text_com" value="" /> x 
    </td> 
    <td width="180"> 
     <textarea name="input[0][quo_definition]" class="input"></textarea> 
    </td> 
    <td> 
     <input type="text" name="input[0][quo_amt]" id="new_amt" class="quantity text_com" value="0" /> 
    </td> 
    <td id="total"> 
     <span id="view_total_0">0</span> 
     <input type="hidden" name="input[0][quo_total]" id="new_total" class="quantity text_com" value="0" /> 
    </td> 
    <td> 
     <select name="input[0][quo_btw]" class="btwselect"> 
      <option value="21%" selected="selected">21%</option> 
      <option value="6%">6%</option> 
      <option value="0%">0%</option> 
     </select> 
    </td> 
</tr> 
</table> 
<br /> 
<div class="ClientProductItemsSubTotal"> 
<table cellpadding="5" cellspacing="0" width="100%"> 
<tr> 
<td><strong>Korting</strong></td><td><input type="text" name="discount" class="quantity" value="" /></td> 
</tr> 
</table> 
</div> 

形式DB $input = array();

foreach($_POST['input'] as $input) { 
$mysqli->query("INSERT INTO jon_com_quo_computation VALUE('', '$uc', '$cc', '$ct', '$cur', '', '".$input['cat_name']."', '".$input['category']."', '".$input['quantity']."', '".$input['quo_definition']."', '".$input['quo_amt']."', '".$input['quo_total']."', '".$input['quo_btw']."', '$dis', '', '', '1', 'new', NOW())"); 
} 

jQuery來添加以下......這應該是在底部的形式...我用.insertAfter(rowfield);

jQuery(function() {  
id = 0; 
jQuery('a#add_new_element').live('click', function() { 
id++; 

var rowfield = jQuery(this).parent().parent();//add some .parent() untill you get the TR element 
jQuery('<tr><td><a id="add_new_element"><img src="images/add.png" width="20" alt="add product" /></a> <a id="add_new_category"><img src="images/cat.png" alt="add category" /></a></td><td><input type="text" name="input['+id+'][category]" class="text_com" value="" /></td><td><input type="text" name="input['+id+'][quantity]" id="new_quan" class="quantity" value="" /> x</td><td width="200"><textarea name="input['+id+'][quo_definition]" class="input"></textarea></td><td><input type="text" name="input['+id+'][quo_amt]" id="new_amt" class="quantity" value="0" /></td><td><span id="view_total_'+id+'">0</span><input type="hidden" id="new_total" name="input['+id+'][quo_total]" class="quantity" value="0" /></td><td><select name="input['+id+'][quo_btw]" class="btwselect"><option value="21%" selected="selected">21%</option><option value="6%">6%</option><option value="0%">0%</option></select></td><td><a id="del_row"><img src="images/del.png" class="DelRow" alt="delete" /></a></td></tr>').insertAfter(rowfield); 

}); 

非常感謝你的分享想法......

編輯:更多解釋... 有兩件事我需要修復。 首先,當它保存到我的數據庫時,有序並不是真的如此。 應該a b c da c b d

第二件事是,我不知道如果我去使用insertAfter()insertBefore() 我的老闆要的是,他可以添加任何形式的 - 當他點擊添加按鈕它應該在下面添加。

例如,當我點擊綠色+添加按鈕時,它應該添加到最後添加的tr。

我公開解釋更多..

+3

仁慈的上帝,我很困惑的! –

+0

即時通訊很抱歉,如果我的英語太混亂了...讓我編輯它,並提出一些更多的解釋... – Butternut

+1

「從jQuery 1.7開始,.live()方法不推薦使用.on()來附加事件handlers.jQuery舊版本的用戶應該使用.delegate()優先於.live()。「 –

回答

1

嘗試在點擊事件附加,而不是insertAfter的使用:

var $table = jQuery(this).closest('table') 
$table.append('<tr>whatever</tr>'); 
+0

讓我嘗試1秒... – Butternut

+0

嗨@HarryFink先生非常感謝你,append現在工作正常。我的上帝,我的帖子太長,唯一的答案很簡單T_T我需要更多的經驗,像你的先生。 – Butternut

+0

我嘗試了我的DB - >保存它現在有序地保存即時通訊這麼高興...再次感謝@HarryFink – Butternut