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

时间:2013-09-24 19:05:31

标签: php forms concrete5 form-processing

我有一个包含订购表单的页面,在此表单上列出了供应商信息,然后产品下方和前面的供应商的每个产品都是一个输入字段,允许用户输入数量他们想要的每种产品。 提交信息后,会进入确认页面,我需要能够显示订单信息。在订单页面上的表单上,我有一个包含供应商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>";
        }
    }
}



?>

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

2 个答案:

答案 0 :(得分:2)

您可能想要重新排列数组,并执行以下操作:

$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中生成的数组的数量为&amp;他们的供应商分成了他们自己的数组。

答案 1 :(得分:0)

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

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

            echo "$vendorid";

    }
}