Whether it's a product, a product, or an item. have corresponding specifications, different products corresponding to the specifications of different, then fill the specifications are different, for some product specifications need to fill in, and there are write specifications do not need to fill in, then the design data field is to avoid unnecessary fields, we can put all the specifications are stored in a field. Let's share the source code below.
1, PHP inserted into the database.
The PHP code is as follows:
$spec = "";
if ($data [' Size ']!= "") {
$spec. = "Size". ":". $data [' Size ']. ",";
}
if ($data [' Weight ']!= "") {
$spec. = "Weight". ":" $data [' weight ']. ",";
}
if ($data [' Diameter ']!= "") {
$spec. = "diameter". ":" $data [' diameter ']. ",";
}
if ($data [' Chang ']!= "") {
$spec. = "Long". ":" $data [' Chang ']. ",";
}
if ($data [' Height ']!= "") {
$spec. = "High". ":" $data [' height ']. ",";
}
$data [' Spec ']=rtrim ($spec, ",");
2, query specification code.
The PHP code is as follows:
<php> $array =explode (', ', $list [' spec ']);
foreach ($array as $k = = $v) {
$arr []=explode (': ', $v);
};
foreach ($arr as $k = = $v) {
$t [$v [0]]= $v [1];
};
</php>
<li class= "" >
<span class= "" > Size </span>
<input class= "cans1" type= "text" <php>if (array_key_exists ("size", $t)) {$s = $t [' Size '];} </php> value= ' {$s} ' name= "size" placeholder= "0"/>
<span class= "cans" > Units (cm) </span>
</li>
<li class= "" >
<span class= "" > Weight </span>
<input class= "Cans2" type= "text" <php>if (array_key_exists ("Weight", $t)) {$z = $t [' Weight '];} </php> value= ' {$z} ' name= "weight" placeholder= "0"/>
<span class= "cans" > Units (g) </span>
</li>
<li class= "" >
<span class= "" > Diameter </span>
<input class= "Cans3" type= "text" <php>if (array_key_exists ("diameter", $t)) {$j = $t [' diameter '];} </php> value= ' {$j} ' name= "diameter" placeholder= "0"/>
<span class= "cans" > Units (cm) </span>
</li>
<li class= "" >
<span class= "" > Long </span>
<input class= "CANS4" type= "text" <php>if (array_key_exists ("Long", $t)) {$c = $t [' Long '];} </php> value= ' {$c} ' name= "Chang" placeholder= "0"/>
<span class= "cans" > Units (cm) </span>
</li>
<li class= "" >
<span class= "" > High </span>
<input class= "CANS5" type= "text" <php>if (array_key_exists ("High", $t)) {$g = $t [' High '];} </php> value= ' {$g} ' name= "height" placeholder= "0"/>
<span class= "cans" > Units (cm) </span>
</li>
So here is the end, now you should know how to store it, if you still have doubts, you can leave a message to consult.
This article by the Professional Zhengzhou app development company Brigitte Xuan Science and Technology finishing release, original is not easy, if need reprint please indicate source.
How to store multiple data in a field in PHP programming