如果选中行,则从表列获取值

时间:2018-11-19 13:20:31

标签: php

我正在使用PHP从表中获取值,并且需要进一步处理它们。 如果选中行,则需要从“数量”和“ FMK代码”列中获取值。

表格:

<table class="table table-bordered">
    <thead>
        <tr class="success">
            <th>#<br/></th> 
            <th>Article  </th>
            <th>Name  </th> 
            <th>Quantity</th> 
            <th>FMK CODE<br/></th>
        </tr>
    </thead>
    <tbody>
        <?php 
            $i = 1; 
            while($r=$q->fetch()){  ?>
            <tr>
                <td><input type="checkbox" class="from-control" name="id[]" value="<?php echo $r['id']?>"></td>
                <td><?=$r['Article']?></td>  
                <td><?=$r['Name']?></td>     
                <td><input type="text" class="form-control" value="<?=$r['quantity'];?>" name="quantity"></td>   
                <td> 
                    <select class="form-control col-lg-2" name="childCode"><?php getChildCodes($r["code"]) ?></select>
                </td>
            </tr>
        <?php } ?>      
    </tbody>
</table>

提交后,我需要从“数量和childCode”中获取所选择的每一行的值。

<button type="submit" name="getValues"> Submit </button>

PhP处理:

<?php 

    if (isset($_POST['getValues'])) {

        $id = $_POST['id'];
        $quantity = $_POST['quantity'];
        $childCode = $_POST['childCode'];

        $values = array(); 

        foreach($id as $id) {
            foreach($quantity as $quant){
                foreach($childCode as $code){
                    array_push($values, $id,$quant,$code);
            }
        }           
        echo "<pre>";
        var_dump($values);
        echo "</pre>";          
    }
?>

值输出。在输出中,无论是否检查了值,我都从表中获取所有值,这是错误的。同样,数组打印也不好,一个名称的值形成数组。我需要将数组ID,数量,子代码的第一个元素形成一个数组。 [0] =>“ 177239”,“ 10.000”,113

array(3) {
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      [0]=>
      string(6) "177239"
      [1]=>
      string(6) "177240"
      [2]=>
      string(6) "177241"
    }
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "10.000"
    [1]=>
    string(7) "100.000"
    [2]=>
    string(6) "10.000"
  }
  [2]=>
  array(3) {
    [0]=>
    string(11) "113"
    [1]=>
    string(10) "87"
    [2]=>
    string(10) "91"
  }
}

1 个答案:

答案 0 :(得分:0)

misorude指出您需要使用一种构建PHP数组的样式来命名表单元素。如前所述,使用[]字符来完成。因此,第一行的元素名称将为<input name="id[0]" ...<input name="quantity[0]" ...。第二行的元素名称为<input name="id[1]"<input name="quantity[1]"

还要确保foreach块不会覆盖$id变量:)

foreach($id as $id) // Oops!
相关问题