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++;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行