Jump to content

[SOLVED] Array Question


gotornot

Recommended Posts

I am creating a form that generates fields dynamically on the fly as per database requirements.

When i submit the form the field names are generated by the database name for the option they are.

 

How do i collect them the other end and insert them into a db?

 

I can add a $vale = $_REQUEST[''];for each one as there will be lots of combinations

 

Is there any quick way of grabbing passed over data and spliting it up?

Link to comment
Share on other sites

Depends what your dynamic fields look like.... if you did something like

 

<input type="text" name="firstname[]" />
<input type="text" name="surname[]" />
<input type="text" name="email[]" />
<input type="text" name="firstname[]" />
<input type="text" name="surname[]" />
<input type="text" name="email[]" />
<input type="text" name="firstname[]" />
<input type="text" name="surname[]" />
<input type="text" name="email[]" />

 

Assuming that you validated that all fields are villed in you can then use something like...

 

$count = count($_POST['firstname']);

foreach($_POST['firstname'] as $k=>$v) {
   echo $v . " " . $_POST['surname'][$k] . " " . $_POST['email'][$k];
}

 

There are other options it all depends on your preferences.

Link to comment
Share on other sites

Hiya

 

well its a custom shopping catr and items have different options ie clothes is size colour and finish some products dont have any they need width and depth so i worte a general script  that added as many options per products as was needed.

 

So i need a way of grabbing all passed data of these options and insertisng them into a db.

Link to comment
Share on other sites

In that case your probably looking for something more along the lines of...

 

<?php
foreach($_POST['items'] as $k=>$v) {
   switch($v['type']) {
      case "name1":
     // parse information to add to db here...
     break;
  case "name2":
     // parse information to add to db here...
     break;
  case "name3":
     // parse information to add to db here...
     break;
   }
}
?>
<br/>
<form action="" method="post">
<input type="text" name="items[0][type]" value="name1" /><input type="text" name="items[0][description]" value="Blah... blah... blah.." /><input type="text" name="items[0][quantity]" value="10" /><br />
<input type="text" name="items[1][type]" value="name2" /><input type="text" name="items[1][description]" value="Some description" /><br />
<input type="text" name="items[2][type]" value="name3" /><input type="text" name="items[2][description]" value="blah..." /><br />
<input type="submit" name="submit" />
</form>

Link to comment
Share on other sites

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.