note 47537 deleted from ref.array by sean
| From: | sean@php.net | Date: | Fri, 19 Nov 2004 16:38:07 +0000 |
| Subject: | note 47537 deleted from ref.array by sean | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80763@lists.php.net to get a copy of this message | ||
Note Submitter: kevin at unison dot ie
----
I was looking for a way to send product array data to a script via POST, the product information
could vary slightly and have elements update or added depending on actions taken by the user. The
data would then have to be repackaged as hidden fields (sessions were not an option) eg
<FORM action="" method="post">
<INPUT name="product[0][product_id]" type="hidden" value="1">
<INPUT name="product[0][product_name]" type="hidden"
value=".com">
<INPUT name="product[0][price]" type="hidden" value="14.99">
<INPUT name="product[0][duration]" type="hidden" value="5">
<INPUT name="product[1][product_id]" type="hidden" value="6">
<INPUT name="product[1][product_name]" type="hidden"
value=".ie">
<INPUT name="product[1][price]" type="hidden" value="89.99">
<INPUT name="submitted" type="hidden" value="true">
<INPUT name="submit" type="submit">
</FORM>
Below is a piece of code that I hope will save someone a bit of time.
$arr_Products = $_REQUEST['product'];
$int_OuterCntr = 0;
foreach($arr_Products as $arr_ProductDetails)
{
foreach($arr_ProductDetails as $p)
{
$arr_Keys = array_keys($arr_ProductDetails, $p);
// gets the keys to be used as the element name
foreach($arr_Keys as $str_Keys)
{
echo'<INPUT name="product['.$int_OuterCntr.']['.
$str_Keys.']" type="hidden" value="'.$p.'">';
}
}
$int_OuterCntr++;
}
----
Reason: useless example