【问题标题】:PHP: Store dynamically generated text field values in databasePHP:将动态生成的文本字段值存储在数据库中
【发布时间】:2016-05-26 06:00:51
【问题描述】:

我有一个数组包含动态生成的文本框值的结果集。

在下面的示例中,我创建了三个动态生成的行,每行包含 6 个文本字段。为了区分每个行名称,我添加了行 ID 作为名称的最后一个单词。示例 ClaimExecutionCountry1 表示 ClaimExecutionCountry 作为名称,1 是行 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
)

我在将这些值存储在数据库中时遇到了一个问题。在我的数据库结构如下

Id  |   ClaimExecutionCountry  |   activitystartdate  |  activityenddate  | CLCode  | SCSCode  |  fileName

我需要在此表中存储= 之后的符号值。

插入表格后,结果为

 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 

所以请任何人帮助我使用上述格式将数组值存储在数据库中。我想你理解我的问题。我正在使用 PHP、codignator 和 MySql 作为数据库。在此先感谢

【问题讨论】:

  • 到目前为止你有什么尝试??
  • 我尝试了许多选项,例如将结果分解为数组并基于计数(在示例中计数为 3)我放入 for 循环。在循环中,我试图分隔每一行。但不工作。

标签: php mysql codeigniter


【解决方案1】:

请尝试以下代码:

$_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*/
        }

希望对你有所帮助。

【讨论】:

【解决方案2】:
$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};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 2011-03-10
    • 2011-03-18
    • 1970-01-01
    • 2013-06-03
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多