1

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



?>

我感谢任何人可以提供的任何帮助。这实际上让我难倒了几天。

4

2 回答 2

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会将数量和他们的供应商分成他们自己的数组。

于 2013-09-24T19:51:43.567 回答
0

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

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

            echo "$vendorid";

    }
}
于 2013-09-24T19:50:52.327 回答