简体   繁体   English

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

[英]Get values from table column if row checked

I am using PHP to get values from the table and need to process them further. 我正在使用PHP从表中获取值,并且需要进一步处理它们。 If row is checked I need to get values from columns Quantity and FMK Code. 如果选中行,则需要从“数量”和“ FMK代码”列中获取值。

Table: 表:

<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>

On submit I need to get values from "quantity and childCode" for each row selected. 在提交时,我需要从“ quantity and childCode”中为所选的每一行获取值。

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

PhP processing: 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>";          
    }
?>

values output. 值输出。 In output I get all values from table, no matter if they are checked or not which is wrong. 在输出中,无论是否检查了值,我都从表中获取所有值,这是错误的。 Also array print was not well, values from one name form array. 同样,数组打印也不好,一个名称的值形成数组。 I need array to be formed first element of id, quantity, child code to b one array. 我需要将数组ID,数量,子代码的第一个元素形成一个数组。 [0]=>"177239","10.000",113 [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"
  }
}

misorude is pointing out that you need to name the form elements in a style that builds a PHP array. misorude指出您需要使用一种构建PHP数组的样式来命名表单元素。 That is done with the [] characters as mentioned. 可以通过上述[]字符来完成。 So the first row would have element names like <input name="id[0]" ... , <input name="quantity[0]" ... . 因此,第一行将具有元素名称,例如<input name="id[0]" ...<input name="quantity[0]" ... The second row would have element names like <input name="id[1]" and <input name="quantity[1]" 第二行将具有元素名称,例如<input name="id[1]"<input name="quantity[1]"

Also make sure the foreach block doesn't overwrite the $id variable :) 还要确保foreach块不会覆盖$id变量:)

foreach($id as $id) // Oops!

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

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