PHP:将动态生成的文本字段值存储在数据库中
I have one array contains the result set from dynamically generated text box values.
In the below example I created three dynamically generated rows and each row contains 6 text field. For differentiate each row name i added the row id as the last word of name. Example ClaimExecutionCountry1
means ClaimExecutionCountry
as the name and 1
is the row id.
Array
(
[0] => ClaimExecutionCountry1=10
[1] => activitystartdate1=05-27-2016
[2] => activityenddate1=06-24-2016
[3] => CLCode1=CLC1
[4] => SCSCode1=SCS1
[5] => fileName1=calc2.png
[6] => ClaimExecutionCountry2=53
[7] => activitystartdate2=05-27-2016
[8] => activityenddate2=05-28-2016
[9] => CLCode2=
[10] => SCSCode2=
[11] => fileName2=gh.png
[12] => ClaimExecutionCountry3=82
[13] => activitystartdate3=05-26-2016
[14] => activityenddate3=07-28-2016
[15] => CLCode3=
[16] => SCSCode3=SCS5
[17] => fileName3=preview1.png
)
I am facing one issue for storing these values in Database. In my database structure is below
Id | ClaimExecutionCountry | activitystartdate | activityenddate | CLCode | SCSCode | fileName
I need to store after =
symbol values in this table.
after insert the table, the result would be
Id | ClaimExecutionCountry | activitystartdate | activityenddate | CLCode | SCSCode | fileName
------------------------------------------------------------------------------------------------------------
1 | 10 | 05-27-2016 | 06-24-2016 | CLC1 | SCS1 | calc2.png
2 | 53 | 05-27-2016 | 05-28-2016 | null | null | gh.png
3 | 82 | 05-26-2016 | 07-28-2016 | null | SCS5 | preview1.png
So Anyone please help me to store the array values in database using above format. I think you understood my problem. I am using PHP,codignator and MySql as database. Thanks in advance
Please try code below:
$_array=Array(
[0] => ClaimExecutionCountry1=10
[1] => activitystartdate1=05-27-2016
[2] => activityenddate1=06-24-2016
[3] => CLCode1=CLC1
[4] => SCSCode1=SCS1
[5] => fileName1=calc2.png
[6] => ClaimExecutionCountry2=53
[7] => activitystartdate2=05-27-2016
[8] => activityenddate2=05-28-2016
[9] => CLCode2=
[10] => SCSCode2=
[11] => fileName2=gh.png
[12] => ClaimExecutionCountry3=82
[13] => activitystartdate3=05-26-2016
[14] => activityenddate3=07-28-2016
[15] => CLCode3=
[16] => SCSCode3=SCS5
[17] => fileName3=preview1.png
)
foreach($_array as $val){
$a = explode("=",$val);
$field = $a[0];
$ans=$a[1];
$matches = array();
if (preg_match('#(\d+)$#', $field, $matches)) {
$rowNum=$matches[1];
}
$fieldName = str_replace($rowNum,"",$field);
/*Now you have number of row , $fieldName , $rowNum and $ans so we can execute SQl statement inside forEach*/
}
Hope it will help you.
$a = Array
(
[0] => ClaimExecutionCountry1=10
[1] => activitystartdate1=05-27-2016
[2] => activityenddate1=06-24-2016
[3] => CLCode1=CLC1
[4] => SCSCode1=SCS1
[5] => fileName1=calc2.png
[6] => ClaimExecutionCountry2=53
[7] => activitystartdate2=05-27-2016
[8] => activityenddate2=05-28-2016
[9] => CLCode2=
[10] => SCSCode2=
[11] => fileName2=gh.png
[12] => ClaimExecutionCountry3=82
[13] => activitystartdate3=05-26-2016
[14] => activityenddate3=07-28-2016
[15] => CLCode3=
[16] => SCSCode3=SCS5
[17] => fileName3=preview1.png
)
Take the count of array------
$cnt = count($a);
<!--now loop it and explode it to get the values after = -->
for($i=0;$i<=$cnt;$i++){
$b = explode("=",$a[i]);
<!--exploded value will give $b[0] = hhjhj and $b[1] = 10 ok -->
$c[] = $b[1];
}
<!-- In $c array all the values came after =.....now -->
$k=0;
$l=6;
$h=0;
for($j=$k;$j<=$l;$j++){
if($j == $l){
$j = $k;
$l = $l + 5;
$h++;
}
<!-- So from this here we got no of set of rows to be inserted in table --it will give result [3] from array $c(mixed values of no of rows) -->
}
<!-- now going to split into rows and insert-->
for($i=1;$i<=3;$i++){
$e=6;
for($j=0;$j<=$e;$j++){
if($j==$e){
$j=$e+1;
$e = $e+6;
break;
}else{
$row[$i] = $c[$j];
}
}
}
<!-- now we got --- -->
$row[1] = {0,date,date,ccsc,sslc,image};
$row[2] = {0,date,date,ccsc,sslc,image};
$row[3] = {0,date,date,ccsc,sslc,image};