Jump to content

Preparing mutiple values for hidden input to form


Lassie

Recommended Posts

I have a requirement to enter values in a form in the following format.

 


<input type="hidden" name="item_name_1" value="Item_name_1">
<input type="hidden" name="amount_1" value="1.00">
<input type="hidden" name="item_name_2" value="Item_name_2">
<input type="hidden" name="amount_2" value="2.00">

In my application the item name = $title and amount=$price

I have code which extracts the title and price but I cant figure out how increment the item_name_x and amount_x by 1, starting from 1, for each item purchased.

My code sofar is:-

$book = array();
 foreach ($cart as $product_id => $qty)
  {
    $book[] = get_book_details($product_id);
    
  }
$book_list ="";
foreach($book as $single_book)
{
	extract($single_book);
	$item_name_1=$title;
	$amount_1=$price;
}
echo"$item_name_1";
echo"$amount_1";
display_pp_form($name,$email);

I ned to pass the values of item and amount to the display_pp_form function.

Any ideas appreciated.

It doesn't need to be that complex:

 

<?php
$fld_idx = 1;

foreach ($cart as $product_id => $qty)
  {
    $book = get_book_details($product_id);

    echo '<input type="hidden" name="item_name_' . $fld_idx . '" value="' . $book["title"] . '">';
    echo '<input type="hidden" name="amount_' . $fld_idx . '" value="' . $book["price"] . '">';

    $fld_idx++;
  }
?>

Whether to put code in-line or in a seperate function is sometimes obvious and sometimes not. The general rules I follow are to pull code into a seperate function if:

 

- I need to reuse the code in multiple places

- The code is complex and creating a seperate function makes it easier to work with the code (especially with edits)

- It it just "makes sense". For example, if I need to process a bit of data before using it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.