仅显示 foreach 循环中的特定数组元素


Display only specific array elements in a foreach loop

我有一个包含订购表单的页面,在此表单上它列出了供应商信息,然后产品下方和前面的供应商的每个产品都是一个输入字段,允许用户输入他们想要的每个产品的数量。提交信息后,进入确认页面,我需要能够在其中显示订单信息。在订单页面上的窗体上,我有一个包含供应商 ID 的隐藏字段,并且每个供应商放置一次供应商 ID。我需要做的不仅是回显数量,还要回显每个订单的特定供应商 ID。我的代码如下。第一个块是订单页面,然后下面的块将是确认页面。现在,它位于每个数量的下方,它显示所有供应商ID,而不仅仅是我需要的供应商ID。

<?php defined('C5_EXECUTE') or die("Access Denied.");?>
<div class="ccm-ui">
<?php
$db= Loader::db(); //This loads the database helper.
Loader::model('user'); //This loads the user Model.
$user = new User();
$userInfo = UserInfo::getByID($user->getUserID()); //This gets the user info for the current user.
$userCostCenter = $userInfo->getAttribute('cost_center'); //This sets a variable equal to the attribute Cost Center for the current user.
//The if statement below checks if the user is an admin and then displays the info accordingly.
if ($userCostCenter === "Admin") {
?>  
    <form name="SelectCostCenter" action="/adminorder" method="POST">
        <select name="CostCenter">
            <option value="unitedilluminating">United Illumination</option>
            <option value="clp">CL&P</option>
        </select>
        <input type="submit" value="Continue">
        <button style="float:right;" type="button" class="btn btn-primary"></button>
    </form>
<?php
} elseif ($userCostCenter === "United Illuminating") {
?>
<form name="OrderForm" action="/confirm" method="POST">
<?php
$query = 'SELECT * FROM Vendors WHERE costCenterID = 1';
$productQuery = 'SELECT * FROM Products WHERE costCenterID =  1';
$results = $db->getAll($query);
$productResults = $db->getAll($productQuery);?>
<table class="table">
    <thead>
        <tr>
            <th>Quantity/Product</th>
            <th>Category</th>
            <th>Vendor</th>
            <th>Address</th>
        </tr>        
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
    <td></td>
    <td><?php echo $vendor['Category']; ?></td>
    <td><?php echo $vendor['Vendor']; ?></td>
    <td><?php echo $vendor['Address']; ?></td>
</tr>
<?php foreach ($productResults as $product) { ?>
<tr class="product">
    <td colspan="4"><span class="name"><input type="text" name="quantities[]" size="1" /><?php echo $product['Product'];?></span></td>
</tr>
<?php } ?>
    <td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>




<?php 
 }?>
 </table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
else {
    ?>
<form name="OrderForm" action="/confirm" method="POST">
    <?php $query = 'SELECT * FROM Vendors Where costCenterID = 2';
        $productquery = 'SELECT * FROM Products WHERE costCenterID =  2';
$results = $db->getAll($query);
$productresults = $db->getAll($productquery);?>
 <table class="table">
              <thead>
                <tr>
                  <th>Quantity/Product</th>
                  <th>Category</th>
                  <th>Vendor</th>
                  <th>Address</th>
                </tr>
<?php
foreach ($results as $vendor) {
?>
<tr class="category">
    <td></td>
    <td><?php echo $vendor['Category'];?></td>
    <td><?php echo $vendor['Vendor'];?> </td>
    <td><?php echo $vendor['Address'];?></td>
    </tr> 
    <?php
 foreach ($productresults as $product){
    ?>
      <tr class="product">
    <td colspan="4"><span class="name"><input type="text" name="quantities[<?php echo $vendor['vendorID']; ?>]" size="1" /><?php echo $product['Product'];?></span></td>
    <td><input type="hidden" name="vendor[]" value="<?php echo $vendor['vendorID']; ?>"/></td>
    </tr>
        <?php
 }
    ?>

<?php 
 }?>
 </table>
<input type="submit" value="Checkout"<button style="float:right;" type="button" class="btn btn-primary"></button>
</form>
</div><?php
}
?>

这是下面的确认页面。

<?php defined('C5_EXECUTE') or die("Access Denied.");
$db= Loader::db();
$quantity = $_POST['quantities'];
$vendor = $_POST['vendor'];
$minimumorder = 25;
foreach($quantity as $num){
    if ($num >= $minimumorder){
        echo "$num";
        echo "</br>";
        foreach($vendor as $vendors){
            echo "$vendors";
            echo "</br>";
        }
    }
}

?>

我感谢任何人能给予的任何帮助。这实际上已经让我难倒了几天。

你可能

想要重新排列你的数组,并执行以下操作:

$i = 0;
foreach ($productresults as $product) {
     echo '<input name="product['.$i.'][quantity]" />';
     echo '<input name="product['.$i.'][vendor_id]" value="'.$vendor['vendorID'].'" type="hidden" />';
     ++$i;
}

$_POST中生成的数组会将数量及其供应商分离到自己的数组中。

在您的代码中,$vendor['vendorID']似乎是您$_POST['quantities']的关键,因此在您的确认页面中您可以使用:

foreach($quantity as $vendorid=>$num){
    if ($num >= $minimumorder){
        echo "$num";
        echo "</br>";
            echo "$vendorid";
    }
}