How to store multiple data in a field in PHP programming

Source: Internet
Author: User
Tags php programming

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.