Display only specific array elements in a foreach loop

6.1k Views Asked by At

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. and the vendor id is put once for each vendor. 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. 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.

<?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.

2

There are 2 best solutions below

0
Bass Jobsen On

In your code $vendor['vendorID'] seems the key of your $_POST['quantities'] so in your confirm page you could use:

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

            echo "$vendorid";

    }
}
3
J_B On

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.