2013-09-24 72 views
1

我有一個頁面包含一個排序表單,在此表單上它列出了供應商信息,然後每個產品的底下和前面的產品是一個輸入字段允許用戶輸入他們想要的每種產品的數量。 提交後,信息會轉到確認頁面,我需要能夠顯示訂單信息。在訂單頁面上的表單中,我有一個隱藏字段,其中包含供應商ID。併爲每個供應商提供一次供應商ID。我需要做的不僅是迴應數量,還會迴應每個訂單特定的供應商ID。我的代碼如下。第一個塊是訂單頁面,然後下面的塊將是確認頁面。 因爲它現在在每一個數量下面都會顯示所有供應商ID,而不僅僅是我需要的。只顯示foreach循環中的特定數組元素

<?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. 
$u = new User(); 
$ui = UserInfo::getByID($u->getUserID()); //This gets the user info for the current user. 

$userCostCenter = $ui->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>"; 
     } 
    } 
} 



?> 

我欣賞任何人都可以給的幫助。這實際上讓我難倒了幾天。

回答

0

在您的代碼$vendor['vendorID']看來你$_POST['quantities']的鍵,以便在確認頁面,您可以使用:

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

      echo "$vendorid"; 

    } 
} 
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中的結果數組將會將其供應商的數量&分離到它們自己的數組中。

+0

我不完全確定究竟如何在我的源代碼中實現這一點。 –

+0

我知道我可以把這個放在我的訂單頁面上,但是我不知道如何處理它, –

+0

與此類似的代碼會替換您當前的'foreach($ productresults as $ product)'。 我認爲如果你轉儲出'$ _POST',你應該看看如何使用這些數據。 –

相關問題