亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用PHP在數據庫中插入多個附加/嵌套數組元素

如何使用PHP在數據庫中插入多個附加/嵌套數組元素

一只斗牛犬 2021-03-30 13:14:17
我有一個頁面,其中包含一些用戶可以填寫的字段。在主選擇器中,用戶只能選擇一些預定義的選項,在自定義選擇器中,用戶可以使用其他選項填充其他字段。有一個按鈕可以追加一層,并再次具有所有這些選項,還有一個保存按鈕,將保存所有已填充的內容。像這樣的東西:Main Selector|_SelectorCustom 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: MainSaving 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正如我所說,我不知道這是否是理想的方法。我也可以一次解決此節省一層的問題,但是我真的希望如此同時保存。讓我知道這篇文章是否需要任何版本。
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

我通過前面提到的JS方法進行了修復。我得到了用戶以JS var形式生成的屬性數量,并以hidden inputde形式傳遞給PHP文件。在PHP文件中,我將此值用作范圍進行迭代。在下一次迭代中,我獲得了上一次迭代的最后一個值,以定義新的起始范圍。


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++;


查看完整回答
反對 回復 2021-04-08
  • 1 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號