2014-01-30 23 views
0

我有這個添加一個循環內,但在3個值

<?php 

$id=0; 

$sql = mysql_query("SELECT item_product,amount,quantity FROM products_added WHERE cookieid=$cookieid") or die(mysql_error()); 

while ($rows = mysql_fetch_array($sql)) { 

$product  = $rows['item_product']; 
$amount   = $rows['amount']; 
$qty  = $rows['quantity']; 

?> 

<input class="itemname" type="text" id="product_<?php echo $id++;?>" value="<?php echo $product;?>"> 
<input class="itemamount" type="text" id="amount_<?php echo $id++;?>" value="<?php echo $amount;?>"> 
<input class="itemqty" type="text" id="qty_<?php echo $id++;?>" value="<?php echo $qty;?>"> 

<?php } ?> 

但它返回

<input class="itemname" type="text" id="product_1" value="Apples"> 
<input class="itemamount" type="text" id="amount_2" value="1.50"> 
<input class="itemqty" type="text" id="qty_3" value="10"> 

<input class="itemname" type="text" id="product_4" value="Bananas"> 
<input class="itemamount" type="text" id="amount_5" value="3.50"> 
<input class="itemqty" type="text" id="qty_6" value="5"> 

但我需要這個

<input class="itemname" type="text" id="product_1" value="Apples"> 
<input class="itemamount" type="text" id="amount_1" value="1.50"> 
<input class="itemqty" type="text" id="qty_1" value="10"> 

<input class="itemname" type="text" id="product_2" value="Bananas"> 
<input class="itemamount" type="text" id="amount_2" value="3.50"> 
<input class="itemqty" type="text" id="qty_2" value="5"> 

我知道我可以使用從我的數據庫行增量id,但我需要它是乾淨的,每次從1開始。

任何幫助將不勝感激。

我知道它有一些循環中的循環,但似乎無法得到邏輯。

乾杯 強尼

回答

2

正如你可能知道$id++相當於$id = $id + 1。您正在爲$id一直寫入新值。

簡單,不增加每次:

<?php 

$id=1; 

$sql = mysql_query("SELECT item_product,amount,quantity FROM products_added WHERE cookieid=$cookieid") or die(mysql_error()); 

while ($rows = mysql_fetch_array($sql)) { 

$product  = $rows['item_product']; 
$amount   = $rows['amount']; 
$qty  = $rows['quantity']; 

?> 

<input class="itemname" type="text" id="product_<?php echo $id;?>" value="<?php echo $product;?>"> 
<input class="itemamount" type="text" id="amount_<?php echo $id;?>" value="<?php echo $amount;?>"> 
<input class="itemqty" type="text" id="qty_<?php echo $id;?>" value="<?php echo $qty;?>"> 

$id++; 

<?php } ?> 
+0

謝謝巴特!它的工作 - 邏輯可以傷害我的大腦。我應該嘗試過。需要閱讀我的更多手冊。 – jonnypixel