Nathan Riley
asked on
Best way to loop PHP and grab variables for insert to mysql
My var_dump on the array that gets passed is:
How would I loop these and still keep their association for inserting.
For example all of the arrays with the "0" key are associated, same with "1", "2".
array(6) {
["_token"]=>
string(40) "zQ2tv4UljUsdwd7MeA6aKQW1PMQBBEOhB1EDFrc5"
["airline"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "4"
[2]=>
string(1) "2"
}
["flightNumber"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "4"
[2]=>
string(1) "6"
}
["departing"]=>
array(3) {
[0]=>
string(18) "07/24/2017 1:58 PM"
[1]=>
string(18) "07/29/2017 1:58 PM"
[2]=>
string(18) "07/26/2017 1:59 PM"
}
["seatNumber"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "4"
[2]=>
string(2) "55"
}
["tripID"]=>
string(2) "22"
}
How would I loop these and still keep their association for inserting.
For example all of the arrays with the "0" key are associated, same with "1", "2".
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think I need a better understanding of your data layout. Can you give me an example of a data set that won't have an equal number of entries? That should be easy enough to overcome, but I would need to know what you mean.
ASKER
You can see the form here, I've updated the names now so I can do a single foreach on the storage to database part.
http://groupplanner.gallitin.com/flight/create?depart=07%2F20%2F2017&return=08%2F05%2F2017&tripID=22&tripName=ABC#
http://groupplanner.gallitin.com/flight/create?depart=07%2F20%2F2017&return=08%2F05%2F2017&tripID=22&tripName=ABC#
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok, yeah that makes sense, will update. Only issue I see is that I need to modify the names slightly as I continue to clone/add more fields:
Page loads it looks like this:
As I clone I need to increase to:
Not sure how to do that though, opened another question on it in case you want to comment: https://www.experts-exchange.com/questions/29046111/jQuery-update-array-name-on-clone.html
Page loads it looks like this:
flight0[]airline[]
As I clone I need to increase to:
flight1[]airline[]
Not sure how to do that though, opened another question on it in case you want to comment: https://www.experts-exchange.com/questions/29046111/jQuery-update-array-name-on-clone.html
No, you don't need to add the numbers at the end, like you are doing. It is implicitly doing that in the form for every one created. Much like it did for the other fields in your original example. Try it, and you will see what I mean.
ASKER
Ok so just
flight[]airline[]
Right? I'll give a try and post var_dump.
flight[]airline[]
Right? I'll give a try and post var_dump.
ASKER
Hmm...well that gives me an output like:
They are all in one big mess without any names or separation.
But when I increment flight0[]['airline'], flight1[]['airline'], flight2[]['airline'] manually in the inspector, then submit I get:
To me the second option is a lot cleaner as they are separated.
array(3) {
["_token"]=>
string(40) "r7tvg2FQcsvBY8vlS5PA4ByDWfWQkm6GZTvg92Bk"
["flight"]=>
array(8) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(18) "07/24/2017 8:53 PM"
[3]=>
string(1) "2"
[4]=>
string(1) "3"
[5]=>
string(1) "1"
[6]=>
string(18) "07/29/2017 8:53 PM"
[7]=>
string(1) "1"
}
["tripID"]=>
string(2) "22"
}
They are all in one big mess without any names or separation.
But when I increment flight0[]['airline'], flight1[]['airline'], flight2[]['airline'] manually in the inspector, then submit I get:
array(4) {
["_token"]=>
string(40) "r7tvg2FQcsvBY8vlS5PA4ByDWfWQkm6GZTvg92Bk"
["flight0"]=>
array(4) {
[0]=>
array(1) {
["'airline'"]=>
string(1) "1"
}
[1]=>
array(1) {
["'flightNumber'"]=>
string(1) "1"
}
[2]=>
array(1) {
["'departing'"]=>
string(18) "07/24/2017 8:58 PM"
}
[3]=>
array(1) {
["'seatNumber'"]=>
string(1) "1"
}
}
["flight1"]=>
array(4) {
[0]=>
array(1) {
["'airline'"]=>
string(1) "3"
}
[1]=>
array(1) {
["'flightNumber'"]=>
string(1) "2"
}
[2]=>
array(1) {
["'departing'"]=>
string(18) "07/29/2017 8:58 PM"
}
[3]=>
array(1) {
["'seatNumber'"]=>
string(1) "2"
}
}
["tripID"]=>
string(2) "22"
}
To me the second option is a lot cleaner as they are separated.
Looks like I wasn't thinking that through correctly. This leads me back to my original solution. I'm not sure why you feel it won't work. Try the original solution with some values and output the result. It shouldn't matter if some variables are null...if it is to be expected. Just check for that in the input data.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Used Jim's comments to help construct my final needed solution.
ASKER
So instead of airline[], flightNumber[] ect....
identifier[airline[]
identifier[flightnumber[]