Jump to content

Recommended Posts

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

@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

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"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.