Hec*_*dow 7 php mysql sql arrays
我希望在我的SQL数据库中将项目作为数组或类似数据类型存储,并且无法找到关于该主题的令人满意的信息
在PHP中,信息将存储为ingredient["$id"]=true or false,其中$ id表示一个成分
所以对于普通面包(请注意,这仍然是伪代码,因为数据输入端尚未启动)
//code that creates array this bit is still theory and will be manually emulated in the db for the time being
$id=1;
while (isset ($_POST["ingredient part of form".$ID])){
if ($_POST["ingredient".$id]==checked){
$p["ingredient"][$id]=true;
}
else{
$p["ingredient"][$id]=false
}
$id++;
}
//the code that gets the values back for the ingredient name we will use a separate array ingredient_n[$id]
echo p[$prod_id]["item"].': '
$id=1
while (isset ($ingredient[$id])){
if ($p[$prod_id]["ingredient"][$id]==true)
if ($id!=1){
echo ', ';
}
echo $ingredient_n[$id];
}
}
echo '.';
Run Code Online (Sandbox Code Playgroud)
这应该产生类似的东西
'普通面包:小麦面粉,水,盐,酵母.
我查看了ENUM和SET,但这会增加新的成分
Eri*_*itz 15
根据主题判断(将PHP数组存储在单个SQL单元中),您应该将其序列化.有标准化的方法
$array["a"] = "Hello";
$array["b"] = "World";
$array["c"] = "!";
$str = serialize($array);
// The contents of $str
// a:3:{s:1:"a";s:5:"Hello";s:1:"b";s:5:"World";s:1:"c";s:1:"!";}
Run Code Online (Sandbox Code Playgroud)
反向使用反序列化
$unserializedString = unserialize($str);
Run Code Online (Sandbox Code Playgroud)
您可以将它用于数组和对象.
| 归档时间: |
|
| 查看次数: |
8048 次 |
| 最近记录: |