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
https://forums.phpfreaks.com/topic/177535-solved-array-question/
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.

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.

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>

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.