显示从数据库到视图的JSON数据
这是数据库中我的表产品和类别的一个示例: tbl_类别显示从数据库到视图的JSON数据,json,codeigniter,foreach,Json,Codeigniter,Foreach,这是数据库中我的表产品和类别的一个示例: tbl_类别 | id|U类别|名称…….| slug| |1……….|吊架…..|吊架| |2.灯| |3……….|商品|商品| |4……….|存储…|存储| tbl_产品 id_产品| id_类别|名称...| slug...|图像 1………| 1………|吊架asd |吊架asd | json 2………| 1………|吊架asd |吊架dsa | json 3………| 1………|吊架asd |吊架das | json 4………| 1………|吊架asd
| id|U类别|名称…….| slug|
|1……….|吊架…..|吊架|
|2.灯|
|3……….|商品|商品|
|4……….|存储…|存储|
tbl_产品
id_产品| id_类别|名称...| slug...|图像
1………| 1………|吊架asd |吊架asd | json
2………| 1………|吊架asd |吊架dsa | json
3………| 1………|吊架asd |吊架das | json
4………| 1………|吊架asd |吊架sad | json
其中图像的内容是json_编码的,如下所示:
id_产品:1
{
"7b8d9fbfe384b1b6e4cfb0da473df8e5": {
"alt": "jhonson hanger",
"caption": "",
"filename": "7b8d9fbfe384b1b6e4cfb0da473df8e5.jpg",
"primary": true
},
"f7d225c85590012f91bad32dd8adaa3d": {
"alt": "jhonson hanger",
"caption": "lorem ipsum lorem ipsum dolor siamet ameticioud",
"filename": "f7d225c85590012f91bad32dd8adaa3d.jpg"
}
}
等等
我想做的第一件事是让每个产品都显示在我的电子商务产品页面上,因此在我的控制器产品中:
function index()
{
$data = array(
"keyword" => "sadasdasd",
"description" => "asdasdasd",
"content" => "product",
"title" => "BALOK Official :: Product"
);
$products = $this->model_product->get_all_products();
$data['products'] = $products;
$this->load->view("product", $data);
}
在我的model_产品中:function get_all_products()
{
$this->db->select
("
tbl_product.name AS prod_name,
images,
tbl_product.slug AS prod_slug,
tbl_categories.slug AS cat_slug
");
$this->db->from("tbl_products");
$this->db->join("tbl_categories", "tbl_categories.id_category = tbl_product.id_category");
$this->db->order_by("prod_name", "ASC");
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return false;
}
}
如何在我的视图中为每个产品显示产品名称、产品代码、目录代码和仅一个图像['filename]
,如果“primary=true”则显示主图像,否则显示第一个图像。数组中的示例可能图像[0]代码>
我已使用以下代码检查字段图像中的数据:
foreach ($products as $prod)
{
$prod->images = json_decode($prod->images);
print_r($prod->images);
}
这显示了stdClass
对象,如下所示:
function getPrimaryImage($images) {
//initialize the finalImage with 'null'
$finalImage = null;
//Iterate over all images
foreach( $images as $image ) {
//check if primary is set and if it is true
if( isset($image->primary) && $image->primary ) {
//if primary set the finalImage and exit the loop
$finalImage = $image;
break;
} else if( $finalImage == null ) {
//if primary is not set or false and the finalImage is not set, then set to the current image (first one)
$finalImage = $image;
}
}
return $finalImage;
}
foreach ($products as $prod)
{
$prod->images = json_decode($prod->images);
$prod->primaryImage = getPrimaryImage($prod->images);
}
stdClass对象
(
[43f8cd2ba0fcb96453b43b36b6a4f759]=>stdClass对象
(
[文件名]=>43f8cd2ba0fcb96453b43b36b6a4f759.jpg
[alt]=>
[标题]=>
[主要]=>1
)
)
stdClass对象
(
[f7d225c85590012f91bad32dd8adaa3d]=>stdClass对象
(
[文件名]=>F7D225C8559001F91BAD32DD8ADAA3D.jpg
[alt]=>jhonson吊架
[说明]=>同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧眼睑同侧
[主要]=>1
)
[7B8D9FBFE384B1B6E4CB0DA473DF8E5]=>stdClass对象
(
[文件名]=>7B8D9FBFE384B1B6E4CBF0DA473DF8E5.jpg
[alt]=>jhonson吊架
[标题]=>
)
)
stdClass对象
(
[29c2100ff85ec538e17c6d68fafbd43d]=>stdClass对象
(
[文件名]=>29c2100ff85ec538e17c6d68fafbd43d.jpg
[alt]=>
[标题]=>
[主要]=>1
)
[8d4ecb9c4dc369febe70019586f3d570]=>stdClass对象
(
[文件名]=>8d4ecb9c4dc369febe70019586f3d570.jpg
[alt]=>
[标题]=>
)
[dc4358c470c33f20206afc180a28ae5b]=>stdClass对象
(
[文件名]=>dc4358c470c33f20206afc180a28ae5b.jpg
[alt]=>
[标题]=>
)
)
那个主题使我困惑
更新。
鉴于此,我写道:
foreach ($products as $prod)
{
echo $prod->prod_name.' - '.$prod->prod_slug.' - '.$prod->cat_slug.'<br>';
}
foreach($prod形式的产品)
{
echo$prod->prod_name.'-'.$prod->prod_slug.-'.$prod->cat_slug.
';
}
它展示了我想要的东西
书柜木材-书柜木材-存储
全新吊架Jhonson-全新吊架Jhonson-吊架
闪光木材-闪光木材-商品
甘棠干巴州达里卡尤-甘棠干巴州达里卡尤-衣架
萨满灯-萨满灯-灯
储存木架-储存木架-储存
木灯-木灯-灯
木灯超越-木灯超越-灯
Yoyo Kayu-boneka Kayu lucu-商品
图像仍然是json格式,我不知道如何操作它,我只是在考虑数组值,在解码后将json数据转换为数组,也许吧?你有充分的理由使用json数据吗?如果没有,只需创建一个名为tbl_products_images的新表,并将其与以前的联接联接。如果要将第一个映像或主映像设置为true
,则需要执行以下操作:
function getPrimaryImage($images) {
//initialize the finalImage with 'null'
$finalImage = null;
//Iterate over all images
foreach( $images as $image ) {
//check if primary is set and if it is true
if( isset($image->primary) && $image->primary ) {
//if primary set the finalImage and exit the loop
$finalImage = $image;
break;
} else if( $finalImage == null ) {
//if primary is not set or false and the finalImage is not set, then set to the current image (first one)
$finalImage = $image;
}
}
return $finalImage;
}
foreach ($products as $prod)
{
$prod->images = json_decode($prod->images);
$prod->primaryImage = getPrimaryImage($prod->images);
}
编辑
我修改了上面的代码以显示文件名:
foreach ($products as $prod)
{
$prod->images = json_decode($prod->images);
$prod->primaryImage = getPrimaryImage($prod->images);
echo $prod->prod_name.' - '.$prod->prod_slug.' - '.$prod->cat_slug.' '.$prod->primaryImage->filename.'<br>';
}
foreach($prod形式的产品)
{
$prod->images=json_解码($prod->images);
$prod->primaryImage=getPrimaryImage($prod->images);
echo$prod->prod_name.'-'.$prod->prod_slug.'-'.$prod->cat_slug.'.$prod->primaryImage->filename.
';
}
将第一个图像设置为最终结果,循环整个列表。如果您发现一个图像的primary
设置为true
,则替换$finalImage
并退出循环
您可以将其放在一个函数中,该函数接受图像
作为参数,并返回$finalImage
作为结果。您的问题让我感到困惑;)。对于stdObject,如果您解码json,您可以选择是将结果作为带有键和值的数组,还是作为带有属性的对象。stdClass只是一个匿名对象。但是你的实际问题是什么?@t.niese我的实际问题是我不知道如何对每个产品只过滤一张图片,并显示它们。如果在json上设置了primary,则显示primary..:(如果你知道我的意思,你能给我举个例子吗?只是为了确保我理解你的意思是正确的。对于某些产品,你的JSON中有不止一个图像,就像你的示例中最后一个stdClass
。从这一点来看,你只有一个图像?(它是否总是第一个?)是的,你说得对。产品可以有多个图像,在这个产品页面中,它显示数据库中的每个产品,并且始终显示第一个产品,或者如果“primary=true”,则显示主图像.hmm可能是因为我很容易在后端管理产品图像,如果图像被删除或更新,我只需将其发布到多个输入帖子['images']并更新图像字段。:phmm关于json_decode呢。我必须将初始化变量和图像的json_decode函数放在循环中的什么位置?我在我的视图中用示例代码更新了上面的问题我更新了代码,但你真的应该自己多做一点。用我向你展示的代码创建一个函数并将其添加到你的循环不应该那么复杂。;)