Lassie Posted April 17, 2007 Share Posted April 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 17, 2007 Share Posted April 17, 2007 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++; } ?> Quote Link to comment Share on other sites More sharing options...
Lassie Posted April 17, 2007 Author Share Posted April 17, 2007 Thank you. Very elegant. I guess I can include this code in the form rather than passing any values to the function. That is the code would be included in the function. Is that right? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 18, 2007 Share Posted April 18, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.