Jump to content

[SOLVED] Pass two(2) arrays, insert into db with variables from both arrays?!?!?


kronck

Recommended Posts

So I’m at a loss…

 

Goal:

To pass two(2) array’s to a page via html checkbox inputs.  i.e. 

 

First we have….

 

<?
for ($i=0; $i<$num_results; $i++)
{
?>
<input type=checkbox name="work_orderIDs[]" value="<? echo mysql_result($result,$i,"work_orderID") ?>"><? echo mysql_result($result,$i,"description") ?>
<br>
<?
}
?>

And later in the script we have…

<?
for ($i=0; $i<$num_results2; $i++)
{
?>
<input type=checkbox name="contractorIDs[]" value="<? echo mysql_result($result2,$i,"company"); ?>"><? echo mysql_result($result2,$i,"company"); ?>
<br>
<?
}
?>

The arrays seem to pass and work.  What I cannot figure out is parsing the two separate arrays so I can do this:

 

$query = "INSERT into bids (propertyID,contractorID,work_orderID) VALUES ('".$_SESSION["propertyID"]."','".$contractorID."','".$work_orderID."')";

The insert requires values from each array.  I will attach the 3 scripts I’m using for reference so you can have a full understand of what I am attempting to do.  I do not want someone to write the entire process for me, I would just like to know how to pull the variables from each array so that the insert would function correctly.

 

Halp!

 

 

[attachment deleted by admin]

I have no opened your files but I am going to give you a few things to check.

 

An array as you may have guessed is a collection of values therefore your insert query needs to loop for each value. A for loop would be best for this as long as you expect to get the same number of values in each array.

 

First thing is to get your array from the post data, so I assume you have done something along the lines of $work_orderids = $_POST['work_orderIDs'];

 

If thats so then in your query string you need to refer to the value index $work_orderids[0], $work_orderids[1], $work_orderids[2] etc etc for every loop you make incrementing the index number by 1 each time your loop (you could use the for loop control variable for this).

I have no opened your files but I am going to give you a few things to check.

 

An array as you may have guessed is a collection of values therefore your insert query needs to loop for each value. A for loop would be best for this as long as you expect to get the same number of values in each array.

 

First thing is to get your array from the post data, so I assume you have done something along the lines of $work_orderids = $_POST['work_orderIDs'];

 

If thats so then in your query string you need to refer to the value index $work_orderids[0], $work_orderids[1], $work_orderids[2] etc etc for every loop you make incrementing the index number by 1 each time your loop (you could use the for loop control variable for this).

 

The function.php file is the one with they mysql insert.  I have not really even gotten to attempt to finalize it or get it working as i am stumped on how to do a mysql insert based on variables from two seperate arrays.

 

To comment on your statements, yes, i realize a for statement would work best for inserting the data.  Again this is not reflected in the function.php.  I plan on coding that after I am able to get at least one variable from each array to work for each insert.  However, it is very possible and likely that each array in this case would have a different number of variables within it.  There may be 3 work_orderIDs and 2 contractorIDs.

 

I also understand retrieving the info with $work_orderids = $_POST['work_orderIDs'], and in fact can retrieve all of the array info.

 

hopefully to re-irritate more clearly:  the mysql insert is based on variables from both arrays.  I would like the insert to apply one insert per workorderID, per contractorID.  The end result needs to have a propertyID, work_orderID, and contractorID, inserted into the database for each selection made from bid_request.php.  The file bid_request.php has two basic sections:  one for selecting multiple work order requests, and another for selecting multiple contractors.  The <form> in bid_request.php just points to end.php, which calls the function in function.php to do the insert.

 

Ok to simplify things...I have one two arrays:

$work_orderIDs
$contractorIDs

the array $work_orderIDs has 3 variables in it.  i.e.

[0] => 4
[1] => 7
[2] => 1

the array $contractorIDs has 2 variables in it.  i.e.

[0] => 3
[1] => 8

I need to insert into a mysql table an work_orderID for each contractorID.  i.e.

insert into bids work_orderID,contractorID VALUES $work_orderIDs[0],$contractorIDs[0] WHERE blah, blah, blah.
insert into bids work_orderID,contractorID VALUES $work_orderIDs[0],$contractorIDs[1] WHERE blah, blah, blah.

insert into bids work_orderID,contractorID VALUES $work_orderIDs[1],$contractorIDs[0] WHERE blah, blah, blah.
insert into bids work_orderID,contractorID VALUES $work_orderIDs[1],$contractorIDs[1] WHERE blah, blah, blah.

insert into bids work_orderID,contractorID VALUES $work_orderIDs[2],$contractorIDs[0] WHERE blah, blah, blah.
insert into bids work_orderID,contractorID VALUES $work_orderIDs[2],$contractorIDs[1] WHERE blah, blah, blah.

of course the arrays are dynamic and could be any length.  I would like a for statement or foreach statement that would handle the inserts.

 

Thanks for the help btw :)

well i feel stupid.  pretty easy solution:

 


for($i = 0; $i < count($work_orderIDs); $i++)
               {
               $work_orderID = $work_orderIDs[$i];

               for($j = 0; $j < count($contractorIDs); $j++){
               $contractorID = $contractorIDs[$j];

               $query = "INSERT into bids (propertyID,contractorID,work_orderID) VALUES ('".$propertyID."','".$contractorID."','".$work_orderID."')";

 

Not all of the code of course, but the important pieces.

 

Thanks all.

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.