简体   繁体   English

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

[英]Display only specific array elements in a foreach loop

I have a page that contains an ordering form, on this form it lists the vendor information and then each of the products for the vendor underneath and in front of the product is an input field that allows the user to input the quantity of each product that they want. 我有一个包含订购表的页面,该表上列出了供应商信息,然后该产品下方和前面的供应商的每种产品是一个输入字段,允许用户输入每种产品的数量他们要。 Upon submitting the information goes to a confirmation page where I need to be able to show the order information. 提交信息后,进入确认页面,我需要能够显示订单信息。 On the form on the order page, I have a hidden field that contains the vendor id. 在订单页面上的表单上,我有一个包含供应商ID的隐藏字段。 and the vendor id is put once for each vendor. 并且为每个供应商放置一次供应商ID。 What I need to be able to do is not only echo out the quantity but also echo out the vendor id specific for each order. 我需要做的不仅是回显数量,还回显每个订单的特定供应商ID。 My code is below. 我的代码如下。 The first block is the order page and then the block below that will be the confirm page. 第一个块是订单页面,然后下面的块将是确认页面。 As it stands right now underneath every quantity it displays all the vendor ids as opposed to just the one I need. 现在,它位于每个数量的下方,显示所有供应商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
}

?>

This is the confirm page below. 这是下面的确认页面。

<?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 appreciate any help anyone can give. 感谢任何人可以提供的帮助。 This has had me stumped for a few days actually. 这实际上让我难过了几天。

you might want to rearrange your array, and do something like: 您可能需要重新排列数组,并执行以下操作:

$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;
}

The resulting array in $_POST would have the quantities & their vendor separated into their own arrays. $_POST的结果数组会将数量和其供应商分成各自的数组。

In your code $vendor['vendorID'] seems the key of your $_POST['quantities'] so in your confirm page you could use: 在您的代码中, $vendor['vendorID']似乎是$_POST['quantities']的键,因此在您的确认页面中,您可以使用:

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

            echo "$vendorid";

    }
}

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

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