2011-08-14 16 views
0

私は注文システムの作成に忙しいです。PHP/HTML - フォーム入力のトラブル - 複数の投稿を1つに変換する

注文ボタンを1つだけ必要とするフォームを作成しようとする際に問題が発生しています。

現在、フォームは各製品の結果です。ユーザは、特定の製品の所望の量を入力する能力を有し、次いで、選択された量に基づいて製品を注文することができる。選択した商品IDと数量がセッションに追加されます。

5つのアイテムがある場合、5つの注文ボタンがあります。

私はこれを単純化しようとしていますが、5つの製品では、製品の選択全体を制御する注文ボタンが1つしかありません。ここで

は、これに関連するコードです:

//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テーブルがクエリされ、結果が見つかった場合は返されます。

このプロセスでは、結果ごとにフォームが作成されます。

私がしようとしているのは、結果セット全体をカバーするようにこのフォームを拡張してから、「オーダー」の入力を1つだけにするということです。ユーザーは本質的にさまざまな製品に数量を入力し、数量が注文に追加されます。

このタスクについてどのようにアドバイスをいただけますか?

これまでどんな情報がありがたいですか、ありがとうございます。

+0

まず、二度同じ 'ID'マークアップ値を使用しないでください:あなたは、その後のような何かを、あなたの処理スクリプトで

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

。彼らは一意でなければなりません。 – yoda

答えて

1

フィールド名で製品識別子をエンコード:

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です。

こうすれば、配列全体を繰り返し処理して、複数のフィールドを処理することができます。

関連する問題