embsupafly Posted March 6, 2006 Share Posted March 6, 2006 Need a bit of help to find the right solution i have a form that posts to a logic php file, the snippet from the form looks like this:[code]$parts_block .= "<option value=\"$part_number\">$part_number: $description ($vendor) \$$price</option>";for ($i=1;$i<=10;$i++) {$table_rows .= " <tr> <td width=\"80%\" class=\"form\"><select name=\"pc[]\">$parts_block</select></td> <td width=\"20%\" class=\"form\"><input type=\"text\" name=\"qpc[]\" size=\"2\"></td> </tr>";}[/code]So it creates 10 table rows with dynamically named select names. It then hits the logic file which has snippets like this:[code]function isv() {foreach($_POST['pc'] as $choices => $value) { if (!empty($value)) { $choices = "pc_" . "$choices"; print "$choices is $value <br>"; }}print "<br><br>";foreach($_POST['qpc'] as $quantities => $value) { if (!empty($value)) { $quantities = "qpc_" . "$quantities"; print "$quantities is $value <br>"; }}}[/code]This prints out the dynamically named form field names with teh associated post value.I need to add this to a database where each $choices $value are associated with each $quantities $value .So in the form if I selected Item bg-456 with a quantity of 6 and item ad-345 with a quantity of 3, it only adds two rows to the table with the associated data. Quote Link to comment https://forums.phpfreaks.com/topic/4252-dynamic-arrays/ Share on other sites More sharing options...
jordie Posted March 7, 2006 Share Posted March 7, 2006 Try something like this:[code]<?function isv() { foreach($_POST['pc'] as $choices => $value) { if (!empty($value) && !empty($_POST['qpc'][$choices])) { // do DB query here // use $value for the current $_POST['pc'] value // and use $_POST['qpc'][$choices] as the current $_POST['qpc'] value // e.g.: mysql_query(" insert into things (partnumber,quantity) VALUES ('".(int)$value."','".(int)$_POST['qpc'][$choices]."')"); } }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4252-dynamic-arrays/#findComment-14916 Share on other sites More sharing options...
embsupafly Posted March 7, 2006 Author Share Posted March 7, 2006 Perfect, it worked. Now for a curve ball. At this point I really do not want it to add these to the database, instead I would like to set them all as Session Vars to where I can add them later to the database after another step or two in the invoice process.How would I get each one of these values with associated quantities into a session var, and then how would I do the insert based on the session vars? Quote Link to comment https://forums.phpfreaks.com/topic/4252-dynamic-arrays/#findComment-15123 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.