PHP While循环数组

时间:2019-02-25 04:42:54

标签: php arrays post while-loop

从数据库中获得所有结果后,我添加了一些额外的数据(“ Prio”)

我使用了$ _POST方法,并将以下结果存储在数组中:

    array (size=4)
  'gereed' => string 'gereed' (length=6)
  1 => 
    array (size=7)
      'prio' => string '1' (length=1)
      'res' => string '456' (length=3)
      'base' => string '190203' (length=6)
      'lot' => string '101' (length=3)
      'split' => string '0' (length=1)
      'sub' => string '0' (length=1)
      'seq' => string '10' (length=2)
  2 => 
    array (size=7)
      'prio' => string '2' (length=1)
      'res' => string '456' (length=3)
      'base' => string '180676' (length=6)
      'lot' => string '10' (length=2)
      'split' => string '0' (length=1)
      'sub' => string '0' (length=1)
      'seq' => string '30' (length=2)
  3 => 
    array (size=7)
      'prio' => string '3' (length=1)
      'res' => string '456' (length=3)
      'base' => string '180676' (length=6)
      'lot' => string '10' (length=2)
      'split' => string '0' (length=1)
      'sub' => string '0' (length=1)
      'seq' => string '60' (length=2)

现在我只有3行,但是可能有10行或更多行。

我想像while循环一样显示这些数据。 这样循环就可以遍历行(1,2,3,xx),我可以像echo $row['prio'];

那样显示它们

也许这是一个简单的问题,但我仍在学习。

编辑! 在输入部分,我获得Prio值。我想在右行更新此值。

UPDATE VMSCHRIP_SIM SET OPERATION_PRIORITY = $_POST['prio'] WHERE BASE = $_POST['base'] AND RESOURCE = $_POST['res'] AND blablabla

类似的东西...

这是我的代码的一部分。

if ($result > 1) {
        $i = 1;
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $TrCode = transactionCodeDirect($Employee, $row["WORKORDER_BASE_ID"], $row["WORKORDER_LOT_ID"], $row["WORKORDER_SPLIT_ID"], $row["WORKORDER_SUB_ID"], $row["SEQUENCE_NO"], $row["RESOURCE_ID"]);                  
            if ($TrCode <> ""){
                $actief = "<p class=text-success>Actief</p>";
            }else{
                $actief = "<p class=text-warning>Klaar om te starten</p>";
            }
            if($row["DESCRIPTION"] == ""){
                $description = strtok(wo_description($row["WORKORDER_BASE_ID"], $row["WORKORDER_LOT_ID"], $row["WORKORDER_SPLIT_ID"]), '(**');
            }else{
                $description = $row["PART_ID"]." : ".$row["DESCRIPTION"];
            }
            if($row["PREV_OP"] == "" and $row["MAT_REMAINING"] == '0'){
                $row["PREV_OP_COMPLETED_QTY"] = $row["CALC_END_QTY"];
            }
            ?>   
                <a href="#" class="list-group-item"> 
                    <div style="text-align:left">
                        <div class="row">
                            <div class="col-lg-1">  
                                <br>
                                <span class="fa-stack fa-1x">
                                    <i class="fa fa-circle-o fa-stack-2x"></i>
                                    <span  class="fa fa-stack-1x"><?php echo $row["OPERATION_PRIORITY"];?></span>
                                </span>
                            </div>
                            <div class="col-lg-1">  
                                <br>
                                    <input type="text" name="<?php echo $i; ?>[prio]" class="form-control" value="" autofocus placeholder="Prio">
                                    <input type="hidden" name="<?php echo $i; ?>[res]" value="<?php echo $row['RESOURCE_ID'];?>">
                                    <input type="hidden" name="<?php echo $i; ?>[base]" value="<?php echo $row['WORKORDER_BASE_ID'];?>">
                                    <input type="hidden" name="<?php echo $i; ?>[lot]" value="<?php echo $row['WORKORDER_LOT_ID'];?>">
                                    <input type="hidden" name="<?php echo $i; ?>[split]" value="<?php echo $row['WORKORDER_SPLIT_ID'];?>">
                                    <input type="hidden" name="<?php echo $i; ?>[sub]" value="<?php echo $row['WORKORDER_SUB_ID'];?>">
                                    <input type="hidden" name="<?php echo $i; ?>[seq]" value="<?php echo $row['SEQUENCE_NO'];?>">
                            </div>
                            <div class="col-lg-4">  
                                <b><?php echo $row["NAME"];?></b><br>
                                <?php echo $row["WORKORDER_BASE_ID"];?>/<?php echo $row["WORKORDER_LOT_ID"];?>.<?php echo $row["WORKORDER_SPLIT_ID"];?>-<?php echo $row["WORKORDER_SUB_ID"];?>:<?php echo $row["SEQUENCE_NO"];?><br>
                                Vorige bewerking: <?php echo $row["PREV_OP"];?><br>
                            </div>
                            <div class="col-lg-6">                          
                                <?php echo $description;?><br>                                  
                                <?php echo intval($row["COMPLETED_QTY"]);?> / <?php echo intval($row["CALC_END_QTY"]);?> (<?php echo intval($row["PREV_OP_COMPLETED_QTY"]);?>) Stuks
                                <div class="tooltip12"><i class="fa fa-question-circle"></i>
                                    <span class="tooltip12text">Aantal gereed / Totaal (Beschikbaar)</span>
                                </div><br>
                                Volgende bewerking: <?php echo $row["NEXT_OP"];?><br>
                            </div>
                            <div class="col-lg-1">
                                <?php if (transactionCodeDirect($Employee, $row["WORKORDER_BASE_ID"], $row["WORKORDER_LOT_ID"], $row["WORKORDER_SPLIT_ID"], $row["WORKORDER_SUB_ID"], $row["SEQUENCE_NO"], $row["RESOURCE_ID"]) <> ""){ ?>
                                    <i class="fa fa-rotate-right fa-4x" text-center"></i><br><b>Running</b>
                                <?php } ?>  
                            </div>                  
                        </div>
                    </div>
                </a>    
        <?php
        $i++;
        }
    }else{
        echo "Result is niet groter dan 1";
    }

2 个答案:

答案 0 :(得分:0)

可能就像这样。

foreach($_POST as $value){
    if(is_array($value)){
        foreach($value as $key => $val){
            // first iteration
            echo $key; // prio
            echo $val; // 1

            // in second iteration, `$val` contains '2'
        }   
    }
}

答案 1 :(得分:0)

由于具有二维数组,因此可以在PHP中使用foreach函数来遍历数组。在下面的代码中,我使用了嵌套的foreach函数来循环二维数组。

echo "<table><tr>th>Key</th><th>Value</th></tr>"; 
foreach($_POST['your_array'] as $value){
    if(is_array($value)){   
           foreach($value as $key => $val){
                echo "<tr>";
                echo "<td>" . $key . "</td>"; // prio
                echo "<td>" . $val . "</td>"; // 1
                echo "</tr>"
            }   

        }
    }
    echo "</table>";