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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀