简体   繁体   English

PHP数组返回相同的值

[英]PHP array returning the same value

I'm stuck on this and I'm not actually seeing where exactly the problem is. 我一直坚持下去,实际上并没有看到问题出在哪里。

I have a bunch of input tags which are placed like that: 我有一堆这样的输入标签:

<div class="col-md-3">
    <label>Material 1</label>
    <input hidden="hidden" name="idMaterial[]" value="13" type="text" />
    <input class="form-control" name="total[]" type="text" />
</div>

<div class="col-md-3">
    <label>Material 2</label>
    <input hidden="hidden" name="idMaterial[]" value="8" type="text" />
    <input class="form-control" name="total[]" type="text" />
</div>

I have 20 input like that, the idea here is that if I write 10 in the Material 1's input field, my DB would receice something like: 我有20个这样的输入,这里的想法是,如果我在Material 1的输入字段中写10,我的数据库将收到类似以下内容的信息:

id         => A.I
idMaterial => 13
total      => 10

But once I run the code, it comes like that (repeating the idMaterial, even if I write in more than 1 input. It always repeat the first): 但是一旦运行代码,它就会变成这样(重复idMaterial,即使我输入了多个输入也是如此。它总是重复第一个):

idMaterial: 13
total: 10

idMaterial: 13
total: 20

That's the code I'm using to receive that form: 那就是我用来接收该表格的代码:

$idMaterial = array();
$total  = array();

if($this->input->post('total')){
    foreach($this->input->post('idMaterial') as $row){
        $idMaterial = $row;

        foreach($this->input->post('total') as $row2){
            $total  = $row2;

            echo 'Material: '. $idMaterial .'<br> Total: '. $total. '<br><br>'; // TESTING THE OUTPUT
            $query = $this->pedido->salvaLabMaterial($total, $idMaterial); // I'M SENDING THE DATA TO MODEL HERE
        }
    }
}

Any tips is very welcome. 任何提示都非常欢迎。

@RiggsFolly @RiggsFolly

That's where I'm sending the posts the way it comes from the HTML form as you said: 这就是我按照您所说的HTML表单发送帖子的地方:

$query = $this->pedido->salvaLabMaterial($this->input->post('total'), 
                                         $this->input->post('idMaterial'), 
                                        );

The model: 该模型:

public function salvaLabMaterial($this->input->post('idMaterial'), $this->input->post('total')){

    $query = $this->db->query(" insert into labpedidomaterial (idMaterial, total) values ('".$this->input->post('idMaterial')."', '".$this->input->post('total')."') ");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM