douchang8219
2014-08-28 17:07
浏览 13
已采纳

我如何在数组中存储表单值?

How would I be able to append data to an array from a form?

A list is generated according to the data found in the database. Each row has a part number and a QTY text field for the user to edit:

+--------+----------+
| Part # | QTY      |
+--------+----------+
| 123    | [0]      |
+--------+----------+
| 124    | [2]      |
+--------+----------+
| 125    | [15]     |
+--------+----------+
|        | [Submit] |
+--------+----------+ 

The logic I have so far is:

If QTY = 0 { 
ignore 
} else {
append values to array object and store array under "parts" session
} 

An issue I'm running into is when the form is submitted, it grabs the last part and qty value in the form. I need to somehow make each row unique for the form and input that form data into array.

I plan on after the user adds all the parts and qtys to the array, the array is then submitted into a database.

Is there an easier way of doing this?

EDIT:

Grabs the part number and submits the qty value via the form.

<input type="text" name="item['.$row['Part'].'][qty]" size="2" value="0"/>

So it would look like this:

123 => 0, 124 => 2, 125 => 15

And if value in form is set, map the array. I also used the array_filter() function to get rid of the 0 qty parts:

function show_Names($e)
    {
        return "$e[qty]";
    }
        if(isset($_POST['item'])) {
        $c = array_filter(array_map("show_Names", $_POST['item']));
    }

And stored it in a session this way. If nothing is set so far, set a new session. If something is set, then just add onto the array:

if(isset($_SESSION['parts'])) {             
        if(isset($c)) {
        $_SESSION['parts']=$_SESSION['parts']+$c;
        }
    } else {
        if(isset($c)) {
        $_SESSION['parts']=$c;
        }
    }

Hope this helps anyone else tackling a similar issue.

图片转代码服务由CSDN问答提供 功能建议

我如何才能从表单向数组追加数据? < p>根据数据库中的数据生成列表。 每行都有一个部件号和一个QTY文本字段供用户编辑:

  + -------- + ---------  -  + \ N | 部分#| 数量| 
 + -------- + ---------- + 
 |  123 |  [0] | 
 + -------- + ---------- + 
 |  124 |  [2] | 
 + -------- + ---------- + 
 |  125 |  [15] | 
 + -------- + ---------- + 
 |  |  [提交] | 
 + -------- + ---------- + 
   
 
 

我到目前为止的逻辑是 :

 如果QTY = 0 {
ignore 
} else {
append数值对象和存储数组在“parts”会话下的值
} 
    
 
 

我遇到的一个问题是提交表单时,它会抓取表单中的最后一部分和数量值。 我需要以某种方式使每一行对于表单都是唯一的,并将表单数据输入到数组中。

我计划在用户将所有部件和qtys添加到数组后,然后将数组提交到数据库中。

是否存在 更简单的方法吗?

编辑:

抓取零件编号并通过表格提交数量值。

 &lt; input type =“text”name =“item ['。$ row ['Part']。'] [qty]”size =“2”value =“0”/&gt; 
  <  / pre> 
 
 

所以它看起来像这样:

  123 =&gt;  0,124 =&gt;  2,125 =&gt;  15 
   
 
 

如果设置了表格中的值,则映射数组。 我还使用 array_filter()函数去除了0个数量的部分:

  function show_Names($ e)
 {
返回 “$ e [qty]”; 
} 
 if(isset($ _ POST ['item'])){
 $ c = array_filter(array_map(“show_Names”,$ _POST ['item']));  
} 
   
 
 

并以这种方式将其存储在会话中。 如果到目前为止未设置任何内容,请设置新会话。 如果设置了某些内容,则只需添加到数组中:

  if(isset($ _ SESSION ['parts'])){
 if(isset($ c))  {
 $ _SESSION ['parts'] = $ _ SESSION ['parts'] + $ c; 
} 
} else {
 if(isset($ c)){
 $ _SESSION ['parts']  = $ c; 
} 
} 
   
 
 

希望这可以帮助其他人处理类似的问题。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchouyi6730 2014-08-28 17:14
    已采纳

    Yes. By naming the fields like:

    <input type="text" name="item[0][name]" />
    <input type="text" name="item[0][email]" />
    
    <input type="text" name="item[1][name]" />
    <input type="text" name="item[1][email]" />
    
    <input type="text" name="item[2][name]" />
    <input type="text" name="item[2][email]" />
    

    And php script:

    function show_Names($e)
    {
      return "The name is $e[name] and email is $e[email], thank you";
    }
    
    $c = array_map("show_Names", $_POST['item']);
    print_r($c);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzi1117 2014-08-28 18:42

    Add square brackets to your textbox name attribute for example:

    <input type="text" name="part[]" />
    <input type="text" name="qty[]" />
    
    <input type="text" name="part[]" />
    <input type="text" name="qty[]" />
    
    <input type="text" name="part[]" />
    <input type="text" name="qty[]" />
    

    You will then have all part no & qty into $_POST['part'] and $_POST['qty']

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题