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

我有一个页面,其中包含一些用户可以填写的字段。在主选择器中,用户只能选择一些预定义的选项,在自定义选择器中,用户可以使用其他选项填充其他字段。有一个按钮可以追加一层,并再次具有所有这些选项,还有一个保存按钮,将保存所有已填充的内容。像这样的东西:


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)

现在它们都具有相同的名称,并将数据保存为数组(例如:)<input name="color[]">。当我将数据保存在页面中时,在追加一些元素之后,我会同时获得所有数据并进行混合。


我知道为什么会这样,并且我想有一种解决方法,但是我不确定是否正确。我想我可以从JavaScript的一层中获取全部元素,然后将此数字传递给PHP,然后可以仅将该值重复此次数。在下一个循环中,我可以在上一个循环停止的地方开始。


极端简化的PHP代码,无需任何查询:


$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>";

    }

}

输出(显示1个主要元素和2个自定义,第一个具有2个属性,最后一个具有3个属性。已刮擦的元素是那些保存不正确的元素。):



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


正如我所说,我不知道这是否是理想的方法。我也可以一次解决此节省一层的问题,但是我真的希望如此同时保存。让我知道这篇文章是否需要任何版本。


一只斗牛犬
浏览 175回答 1
1回答

qq_遁去的一_1

我通过前面提到的JS方法进行了修复。我得到了用户以JS var形式生成的属性数量,并以hidden inputde形式传递给PHP文件。在PHP文件中,我将此值用作范围进行迭代。在下一次迭代中,我获得了上一次迭代的最后一个值,以定义新的起始范围。if (isset($lastValue)) {&nbsp; &nbsp; &nbsp; &nbsp; foreach (range($lastValue+1, $lastValue+$classNumber[$x]) as $indexValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $key2 = array_search($indexValue, $attrValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "____Nome da Classe " . $value2 . " | Nome do atributo: " . $attrName[$key] . " | Valor do Atributo: " . $attrValue[$indexValue] . " | Cor do Atributo: " . $color[$indexValue] . "<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $lastValue = $lastValue+$classNumber[$x];} else {&nbsp; &nbsp; &nbsp; &nbsp; foreach (range(0, $classNumber[$x]-1) as $indexValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $key2 = array_search($indexValue, $attrValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "____Nome da Classe " . $value2 . " | Nome do atributo: " . $attrName[$key] . " | Valor do Atributo: " . $attrValue[$indexValue] . " | Cor do Atributo: " . $color[$indexValue] . "<br>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $lastValue = $classNumber[$x]-1;}$x++;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript