Jump to content

Recommended Posts

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.


Link to comment
https://forums.phpfreaks.com/topic/4252-dynamic-arrays/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/4252-dynamic-arrays/#findComment-14916
Share on other sites

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?
Link to comment
https://forums.phpfreaks.com/topic/4252-dynamic-arrays/#findComment-15123
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.