I want to create a loop (the result will be inserted into javascript)
Syntax:
So this is finally what the loop should return:
subject_1_1: $("#subject_1_1").val(),
subject_2_1: $("#subject_2_1").val(),
subject_3_1: $("#subject_3_1").val(),
subject_4_1: $("#subject_4_1").val(),
subject_5_1: $("#subject_5_1").val(),
subject_1_2: $("#subject_1_2").val(),
subject_2_2: $("#subject_2_2").val(),
subject_3_2: $("#subject_3_2").val(),
subject_4_2: $("#subject_4_2").val(),
subject_5_2: $("#subject_5_2").val(),
subject_1_3: $("#subject_1_3").val(),
until
subject_5_11: $("#subject_5_11").val()
The last element shouldn't have a comma.
// first sets x = 1, then, while x isn't equal to 5 the code
// inside `{ ... }` is executed and x is added 1
for($x=1;$x<=5;$x++){
// again, but this time with y, from 1 to 11.
for($y=1;$y<=11;$y++){
// So we end up here and we now this code is going to execute
// elevent times for each x value, and a total of 5 x values.
// thats (x = from 1 to 5) * (y = from 1 to 11).
// This string is printed (`echo`) every time for each iteration.
echo "subject_1_3: $(\"#subject_".$x."_".$y."\").val()".(!($x==5&&$y==11)?",":"")."
";
}
}
Edited 1: now removes last comma.
Edited 2: added comments, but read:
In the string being printed, I notice this code:
!($x==5&&$y==11)?",":""
That does the following:
if this is true ? this is executed : and if it is not, then this is executed
So, in pseudo:
(if not (x=5 and y = 11)) then (print comma) else (don't)
Why not do it in the JavaScript itself?
var obj, x, y;
obj = {}; // this will be the object your subject_X_Y properties belong to
for( x=1; x<=5; x++) {
for( y=1; y<=11; y++) {
obj['subject_'+x+'_'+y] = $('#subject_'+x+'_'+y).val();
}
}