Jump to content


Photo

Dynamic arrays?


  • Please log in to reply
2 replies to this topic

#1 embsupafly

embsupafly
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 06 March 2006 - 08:48 PM

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:


$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>";
}


So it creates 10 table rows with dynamically named select names.

It then hits the logic file which has snippets like this:


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>";
    }
}

}


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.



Eric

#2 jordie

jordie
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 March 2006 - 02:57 AM

Try something like this:

<?
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]."')");
        }
    }
}
?>


#3 embsupafly

embsupafly
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 07 March 2006 - 07:15 PM

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?
Eric




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users