2011-08-14 28 views
0

我忙於創建訂單系統。PHP/HTML - 表單輸入問題 - 將多個帖子轉換爲一個

我有問題試圖創建只需要一個訂單按鈕的窗體。

當前表格是針對每個產品的結果。用戶可以輸入所需數量的產品,然後根據所選數量訂購產品。然後將選定的商品標識和數量添加到會話中。

問題在於,如果有5個項目,則有5個順序按鈕。

我想簡化這個,在5種產品中,只有一個訂單按鈕控制整個產品的選擇。

這裏是代碼與此:

//Now we search for our search term, in the field the user specified 

$dataQuery = "SELECT * FROM `products` WHERE upper(`desc`) LIKE '%" 
    . implode("%' AND upper(`desc`) LIKE '%", $keywords_array) 
    . "%' ORDER BY `desc`"; 

$data = mysql_query($dataQuery) or die(mysql_error()); 

$tempVar = 0; 
//And we display the results 
while ($result = mysql_fetch_array($data)) { 
$prId = $result['id']; 
$prRefCode = $result['refCode']; 
$prDesc = $result['desc']; 
$prPack = $result['pack']; 
$prMeasure = $result['measure']; 
$prQuantity = $result['quantity']; 
$prDeptCode = $result['deptCode']; 
$prTaxable = $result['taxable']; 
$prPrice1 = $result['price1']; 
$prPrice2 = $result['price2']; 
$prCrdCode = $result['crdCode']; 
$prCost1 = $result['cost1']; 
$prCost2 = $result['cost2']; 

if ($tempVar == 0) { 
$pageContent .= ' 
<p><u>All prices are inclusive of VAT</u></p> 
<table class="searchResults" border="1" align="center" width="90%"> 
<thead> 
    <tr> 
     <th>Stock Code</th> 
     <th>Description</th> 
     <th>Packsize</th> 
     <th>Price</th> 
     <th>In-Stock?</th> 
     <th>Quantity</th> 
     <th>Submit</th> 
    </tr> 
</thead> 
<tbody> 
'; 
} 

    $pageContent .= ' 
<tr> 
    <td>' . $prId . '</td> 
    <td>' . $prDesc . '</td> 
    <td>' . $prPack . 'x' . $prSize . ' ' . $prMeasure . '</td> 
    <td>R' . $prPrice1 . '</td> 
'; 
    if (empty($prQuantity)) { 
     $pageContent .= ' 
<td>No</td> 
'; 
    } else { 
     $pageContent .= ' 
<td>Yes</td> 
'; 
    } 
    $pageContent .= ' 
    <form id="validated" action="" method="post"> 
    <td>    
      <div> 
       <input type="text" 
onkeydown="return (event.ctrlKey || event.altKey 
       || (47<event.keyCode && event.keyCode<58 && event.shiftKey==false) 
       || (95<event.keyCode && event.keyCode<106) 
       || (event.keyCode==8) || (event.keyCode==9) 
       || (event.keyCode>34 && event.keyCode<40) 
       || (event.keyCode==46))" 
       name="quantity" size ="2" 
       value ="1" 
       style="background: #F4F4F4; 
         font-family: Monaco, monospace;" /> 
      </div>    
    </td> 
    <td> 
      <div> 
       <input type="hidden" name="id" value="' . $prId . '" /> 
       <input type="submit" name="action" value="Order" /> 
      </div>    
    </td> 
    </form>  
</tr> 
'; 
$tempVar ++; 
} 



//This counts the number of results - and if there wasn't any it gives them a little message explaining that 
$anymatches = mysql_num_rows($data); 
if ($anymatches == 0) { 
    $pageContent .= ' 
<p>Sorry, but we can not find an entry to match your query</p> 
<!-- end .aliLeft --></div> 
'; 
} 

if ($anymatches > 0 and count($tempVar) == count($anymatches)) { 
    $pageContent .= ' 
</tbody> 
</table> 
<!-- end .aliLeft --></div> 
'; 
} 

這裏是代碼控制用戶點擊的訂購按鈕實例:

if (!isset($_SESSION['order'])) 
{ 
$_SESSION['order'] = array(); 
} 

if (!isset($_SESSION['quantity'])) 
{ 
$_SESSION['quantity'] = array(); 
} 

if (isset($_POST['action']) and $_POST['action'] == 'Order' and $_POST['quantity'] > 0) 
{ 
// Add item to the end of the $_SESSION['order'] array 
$_SESSION['order'][$_POST['id']] = $_POST['id']; 
$_SESSION['quantity'][$_POST['id']] = $_POST['quantity']; 
header('Location: .'); 
exit(); 
} 

這裏是代碼,用於配置總訂單的數量值:

$total = 0; 
if (count($order) > 0) 
{ 
foreach ($order as $product) 
{ 
    mysql_data_seek($totalsSql, 0); //<- this line, to reset the pointer for every EACH. 
while($row = mysql_fetch_assoc($totalsSql)) 
    { 
    $prodId = $row['id']; 
    $prodPrice1 = $row['price1']; 
    $prodQuantity = $quantity[$prodId]; 
    if ($product == $prodId) 
     { 
     $total += ($prodPrice1*$prodQuantity); 
     break; 
     } 
    } 
} 
} 

正如您所看到的,當用戶搜索產品,查詢mysql表,如果找到結果,則返回它們。

在此過程中,將爲每個結果創建表單。

我想要做的是擴展這個表單來覆蓋整個結果集,然後只有一個輸入用於「Order」,所以基本上,用戶可以在各種產品上輸入數量,然後讓這些產品和數量添加到他們的訂單。

有沒有人有任何建議,我會如何去做這項工作?

我將不勝感激任何輸入,所以永遠謝謝!

+0

首先,不要使用相同的'ID'標記值的兩倍。它們應該是唯一的。 – yoda

回答

1

編碼的字段名的產品標識:

<input type="text" onkeydown="…" name="quantity<?php echo $prId; ?>" size ="2" value ="1" style="…" /> 

在你的流程腳本,那麼你做這樣的事情:

foreach ($_POST as $sKey => $sQuantity) { 
    if (preg_match('/^quantity([1-9][0-9]*)$/DX', $sKey, $asMatch) === 1) { 
     // User added $sQuantity times product $asMatch[1] 
    } 
} 
3

我會將每個輸入的名稱更改爲quantity[$id],其中id是每個字段的ID。你會得到這個結果作爲php中的關聯數組,其中關鍵是你在[]之間提供的id。

這樣,您可以迭代數組並處理多個字段。

相關問題