WatsonN Posted August 14, 2011 Share Posted August 14, 2011 I'm working on a dynamically expanding form and that works just fine, for now. I searched the forums and the manual and can't figure out how to make this work right. There are three fields Title Artist and Album. That submits into a form and then needs to be put into a database. The problem is, I don't know how to correctly form the loop to do it. I tried variations of foreach($_POST['Title'] as $key => $Title) { foreach($_POST['Artist'] as $key => $Artist) { foreach($_POST['Album'] as $key => $Album) { } } } and foreach($_POST['Title'] as $key => $value) { if( !empty($_POST['Artist'][$key]) ){ $Artist = intval($_POST['Artist'][$key]); }else{ $Artist = 0; } if( !empty($_POST['Album'][$key]) ){ $Album = intval($_POST['Album'][$key]); }else{ $Album = 0; } if($Artist > 0) { $arr[] = "('$value','$Artist','$Album')"; } } But I can't get it to a usable state. Any ideas or something I might have missed? Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/ Share on other sites More sharing options...
sunfighter Posted August 14, 2011 Share Posted August 14, 2011 ... I don't know how to correctly form the loop to do it. To do what? You don't need a loop to insert into a DB or into a form. Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/#findComment-1257335 Share on other sites More sharing options...
Psycho Posted August 14, 2011 Share Posted August 14, 2011 What does your form look like? Doing the nested foreach loops probably is the wrong process, but the right approach would depend on the "context" of the data as it is entered onto the form. Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/#findComment-1257338 Share on other sites More sharing options...
sasa Posted August 14, 2011 Share Posted August 14, 2011 2nd way is correct are you shure that $Artist is numeric? Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/#findComment-1257339 Share on other sites More sharing options...
WatsonN Posted August 14, 2011 Author Share Posted August 14, 2011 @sunfighter to insert it into a DB @sasa artist is not numeric The form: <form name="Songs" id="Songs" method="post" action="testpost.php"> <div id="form"> <div class="grid_5"> <input type="text" name="Title[]" /> </div> <div class="grid_5"> <input type="text" name="Artist[]" /> </div> <div class="grid_5"> <input type="text" name="Album[]" /> </div> </div> <div class="grid_16"> <input type="button" value="Add 5 more rows" onClick="addInput('form');"> <input type="submit" value="Submit"> And it will expand as needed up to 200 rows Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/#findComment-1257382 Share on other sites More sharing options...
sasa Posted August 14, 2011 Share Posted August 14, 2011 if you change $Artist in intval it become 0 and your last if is NOT pass Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/#findComment-1257384 Share on other sites More sharing options...
WatsonN Posted August 14, 2011 Author Share Posted August 14, 2011 Yep, I messed that one up. I tried to take a post from a diffrent thread and it was something like this: foreach($_POST['resource_from_label'] as $key => $value) { $quantity = (!empty($_POST['quantity'][$key])) ? intval($_POST['quantity'][$key]) : 0; if($quantity > 0) { $arr[] = "('$value','$quantity')"; } } $arr_to_string = 'Item, quantity ' . implode(' , ',$arr); echo $arr_to_string; But now looking at it his second was quantity and was a number not an actual text value The post I found: "What is the easiest way to make this function" Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/#findComment-1257393 Share on other sites More sharing options...
WatsonN Posted August 14, 2011 Author Share Posted August 14, 2011 Now that that is figured out, what is the best way to go about this once the foreach is opened? I read the manual and all the user comments and I couldn't find something like I needed. Link to comment https://forums.phpfreaks.com/topic/244735-dynamic-form-to-foreach-loop/#findComment-1257397 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.