dougu2036 2019-03-27 14:06
浏览 69
已采纳

如何使用PHP在DB中插入多个附加/嵌套数组元素

I have a page with some fields that the user can fill. In the main selector the user can only select some predefined options, in the custom selector the user can fill other fields, with more detailed options. There is a button that append one more layer, with all these options again, and a save button that will save all the things that was filled. Something like this:

Main Selector
|_Selector

Custom Selector
|_Name
|_Type
|_Attribute
|_numOfOptions (When selected, opens a number of fields related to number)
  |_value
  |_color

(New Layer button)
(Save Button)

Now they all have the same name and save the data as an array (ex: <input name="color[]">). When I save the data in the page, after append some elements, I get the all the data the same time and mixed.

I know WHY this is happening, and I imagine one way to solve this, but I'm not sure if is the correct way. I think I can get the total elements in one layer from JavaScript and pass this number to PHP, then I can iterate the value only this number of times. In the next loop I can start where the previous loop stopped.

Extreme simplified PHP code, without any query:

$nomeCamada = $_POST['nomeCamada'];
$attrName = $_POST['attrName'];
$tipoSelector = $_POST['tipoSelector'];
foreach($nomeCamada as $value2) {
    $key = array_search($value2, $nomeCamada);
    echo "Saving in DB...  CustomSelector: " . $value2 . " | Attr name: " . $attrName[$key] . " | Type: " . $tipoSelector[$key] . "<br>";
    $attrValue = $_POST['attrValue'];
    $color = $_POST['color'];
    foreach ($attrValue as $value3) {
        $key2 = array_search($value3, $attrValue);
        echo "____Class name " . $value2 . " | Attr name: " . $attrName[$key] . " | Attr value: " . $value3 . " | Attr color: " . $color[$key2] . "<br>";
    }
}

Output (Showing 1 main element and 2 custom, the first with 2 attributes and the last with 3. The scratched elements are those who are saving incorrectly.):


Saving in DB... Selector: Main
Saving in DB... CustomSelector: CustomLayer1 | Attribute: attr1 | Type: type1
____Class name: CustomLayer1 | Attr name: attr1 | Attr value: 2 | Attr color: #ff0000
____Class name: CustomLayer1 | Attr name: attr1 | Attr value: 4 | Attr color: #000000
____Class name: CustomLayer1 | Attr name: attr1 | Attr value: 10 | Attr color: #ffff00
____Class name: CustomLayer1 | Attr name: attr1 | Attr value: 20 | Attr color: #800040
____Class name: CustomLayer1 | Attr name: attr1 | Attr value: 30 | Attr color: #8000ff
Saving in DB... CustomSelector: CustomLayer2 | Attribute: attr2 | Type: type2
____Class name: CustomLayer2 | Attr name: attr2 | Attr value: 2 | Attr color: #ff0000
____Class name: CustomLayer2 | Attr name: attr2 | Attr value: 4 | Attr color: #000000
____Class name: CustomLayer2 | Attr name: attr2 | Attr value: 10 | Attr color: #ffff00
____Class name: CustomLayer2 | Attr name: attr2 | Attr value: 20 | Attr color: #800040
____Class name: CustomLayer2 | Attr name: attr2 | Attr value: 30 | Attr color: #8000ff

As I say, I don't know if this is the ideal way to do this. I'm also can fix this saving one layer at a time, but I really want so save all the same time. Let me know if this post needs any edition.

  • 写回答

1条回答 默认 最新

  • dslfjrmz70457 2019-03-28 13:14
    关注

    I fixed by the JS approach I mentioned earlier. I got the number of attributes generated by the user as a JS var and passed to the PHP file as a hidden input, inside de main form. In the PHP file I iterated using this value as range. In the next iteration I got the last value of the previous iteration to define the new starting range.

    if (isset($lastValue)) {
            foreach (range($lastValue+1, $lastValue+$classNumber[$x]) as $indexValue) {
                $key2 = array_search($indexValue, $attrValue);
                echo "____Nome da Classe " . $value2 . " | Nome do atributo: " . $attrName[$key] . " | Valor do Atributo: " . $attrValue[$indexValue] . " | Cor do Atributo: " . $color[$indexValue] . "<br>";              
            }
            $lastValue = $lastValue+$classNumber[$x];
    
    } else {
            foreach (range(0, $classNumber[$x]-1) as $indexValue) {
                $key2 = array_search($indexValue, $attrValue);
                echo "____Nome da Classe " . $value2 . " | Nome do atributo: " . $attrName[$key] . " | Valor do Atributo: " . $attrValue[$indexValue] . " | Cor do Atributo: " . $color[$indexValue] . "<br>";
            }
            $lastValue = $classNumber[$x]-1;
    }
    
    $x++;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题