2011-04-15 66 views
1

這裏是我的形式組織接線柱陣列

<form name="input" action="http://localhost/shopper/index.php?route=module/cart/insert_shopper" method="post"> 
<input quantity="4" type="hidden" name="28" value="1"> 
<input type="hidden" quantity="3" name="29" value="1"> 
<input type="submit" value="Submit" /> 

我有這種格式

[post] => Array 
    (
     [28] => 1 
     [29] => 1 
    ) 

我真正想要的職位陣列是產品,然後對產品ID的列表數量...有沒有一種簡單的方法來改變形式

所以在下一頁我可以做到這一點

$products = $_POST['products'] 
foreach($products as $p) 
{ 
if(isset($p) && $p<>'') 
{ 
///// place your code here 
} 
} 

回答

3
<input type="hidden" name="products[29][3]" value="1"> 


$products = $_POST['products'] 
foreach($products as $product_id=>$quantity) 
{ 
echo $product_id; 
echo $quantity; 
} 
0
<input type="hidden" name="28" value="4"> 
<input type="hidden" name="29" value="3"> 

會給你

[post] => Array 
    (
     [28] => 4 
     [29] => 3 
    ) 
2

在你的HTML表單可以將名字屬性值結束後添加陣列括號,如:

<input type = 'hidden' name = 'products[]' value = '12'/> 
    <input type = 'hidden' name = 'qty12' value = '16'/> 
<input type = 'hidden' name = 'products[]' value = '13'/> 
    <input type = 'hidden' name = 'qty13' value = '72'/> 
<input type = 'hidden' name = 'products[]' value = '14'/> 
    <input type = 'hidden' name = 'qty14' value = '1'/> 

接收腳本中的PHP將把$_POST['products']視爲一個陣列年。數量可看:

foreach($_POST['products'] as $k => $v) { 
    echo($_POST['qty'.$v] . "<br/>"); 
} 
1
<form name="input" action="" method="post"> 
    <input type="hidden" name="29" value="3"> 
    <input type="hidden" name="28" value="4"> 
    <input type="hidden" name="27" value="2"> 
<input type="submit" value="Submit"/> 

<?php 

if (!empty($_POST)) 
{ 
    foreach ($_POST as $id => $quantity) 
    { 
     echo 'ID: '.$id.' quantity: '.$quantity; 
    } 
}