Overall Project:
Save multiple arrays from AJAX
Iterating through the array in PHP:
I've managed to successfully save 2 arrays by using array_combine
and foreach (see example), but now
Problem:
I have 20+ arrays of dynamic content that I need to iterate into my query statement.
Successful AJAX Sample
var setTitle = new Array();
$('form input[name="setTitle[]"]').each(function () {
setTitle.push($(this).val());
});
var setUnitId = new Array();
$('form input[name="setUnitId[]"]').each(function () {
setUnitId.push($(this).val());
});
$.ajax({
type: 'POST',
url: 'form_process2_wkout.php',
data: {
setTitle:setTitle,
setUnitId:setUnitId,
// ...and so on...//
},
dataType: 'json',
cache: false,
async: false
PHP (form_process2_wkout.php) is receiving each $_POST... I've combined them into an array...
$setArray = array(
$x_set_title,
$x_set_unit_id,
// ...and so on... //
);
I've var_dump'ed this array to test...
$data= var_dump($setArray);
echo $data;
It produces the following:
array(24) {
[0]=>
array(3) {
[0]=>
string(9) "Ab Roller"
[1]=>
string(16) "Slide Back Lunge"
[2]=>
string(10) "Jump Squat"
}
[1]=>
array(3) {
[0]=>
string(1) "2"
[1]=>
string(3) "879"
[2]=>
string(3) "872"
}
...and so on...
}
Not quite sure how write to foreach
to iterate through these items:
foreach($setArray as $id => $key) {
$set_title = mysqli_real_escape_string($dbc, $_POST['setTitle']);
$set_unit_id = mysqli_real_escape_string($dbc, $_POST['setUnitId']);
//...and so on.../
}
So that I can use them in the following query:
$sql= "INSERT INTO ".$table1." (`goal_unit_id`,`goal_title`,...and so on...)
VALUES ";
$sql.="('".$set_unit_id."','".$set_title."',...and so on...)";
I've referenced a number of other approaches, but no luck:
PHP: Iterate over multiple arrays and build SQL INSERT query