I have two sets of data which are in two different arrays and I need to combine them in groups then insert the results into my database.
// name values $arr=['Ram','joy','Rahul','Monty']; // values that need to be split between names $code=['10','11','12','13','14','15','16','17','18','19','20','21'];
In above I have two set of data.$arr
contains the name
and $code
contains a numeric code
.
Here I need to insert the code per name into the table as per the user input. Suppose $userinput=2
the first 2 code will insert for name Ram
in two row, then second two will insert for name joy
and so on and if $userinput=3
it will insert accordingly. My expected output for $userinput=3
is given below.
_________________ id name code ---+--------+----- 1 | Ram | 10 ---+--------+----- 2 | Ram | 11 ---+--------+----- 3 | Ram | 12 ---+--------+----- 4 | Joy | 13 ---+--------+----- 5 | Joy | 14 ---+--------+----- 6 | Joy | 15 ---+--------+----- 7 | Rahul | 16 ---+--------+----- 8 | Rahul | 17 ---+--------+----- 9 | Rahul | 18 ---+--------+----- 10 | Monty | 19 ---+--------+----- 11 | Monty | 20 ---+--------+----- 12 | Monty | 21 ---+--------+-----
Advertisement
Answer
here it is…
I hope this helps you… I’m not on my laptop so this is what I could do..
<?php $user_input = 3; // this should be your user input. $arr=['Ram','joy','Rahul','Monty']; $codes=['10','11','12','13','14','15','16','17','18','19','20','21']; $counter1=0; //counter for your $codes to continue. foreach($arr as $name){ $counter = 0; //counter for your condition... for($x=0; $x <= count($codes); $x++){ $counter++; if($counter <= $user_input){ echo "INSERT INTO sampletable (`name`, `code`) VALUES ($name, $codes[$counter1])"."<br />"; }else{ break; } $counter1++; } }