这是我在数据库中的表产品和类别的示例:
tbl_categories
|id_category | name........| slug........|
|1...........| Hanger .....| hanger .....|
|2...........| Lamp .......| lamp .......|
|3...........| Merchandise | merchandise |
|4...........| Storage ....| storage ....|
tbl_products
id_products | id_category | name .....| slug .....| images
1 ..........| 1 ..........| Hanger asd| hanger-asd|json
2 ..........| 1 ..........| Hanger asd| hanger-dsa|json
3 ..........| 1 ..........| Hanger asd| hanger-das|json
4 ..........| 1 ..........| Hanger asd| hanger-sad|json
其中图像的内容是json_encoded,如下所示:
id_product: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_product中:
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;
}
}
如何在我的视图中为每个产品显示产品名称,prod_slug,cat_slug和只有一个 images['filename]
,如果“primary = true”,则显示主图像显示第一个图像。数组中的示例可能是images[0];
。
我已使用以下代码检查了字段图像中的数据:
foreach ($products as $prod)
{
$prod->images = json_decode($prod->images);
print_r($prod->images);
}
并显示stdClass
这样的对象:
stdClass Object
(
[43f8cd2ba0fcb96453b43b36b6a4f759] => stdClass Object
(
[filename] => 43f8cd2ba0fcb96453b43b36b6a4f759.jpg
[alt] =>
[caption] =>
[primary] => 1
)
)
stdClass Object
(
[f7d225c85590012f91bad32dd8adaa3d] => stdClass Object
(
[filename] => f7d225c85590012f91bad32dd8adaa3d.jpg
[alt] => jhonson hanger
[caption] => lorem ipsum lorem ipsum dolor siamet ameticioud
[primary] => 1
)
[7b8d9fbfe384b1b6e4cfb0da473df8e5] => stdClass Object
(
[filename] => 7b8d9fbfe384b1b6e4cfb0da473df8e5.jpg
[alt] => jhonson hanger
[caption] =>
)
)
stdClass Object
(
[29c2100ff85ec538e17c6d68fafbd43d] => stdClass Object
(
[filename] => 29c2100ff85ec538e17c6d68fafbd43d.jpg
[alt] =>
[caption] =>
[primary] => 1
)
[8d4ecb9c4dc369febe70019586f3d570] => stdClass Object
(
[filename] => 8d4ecb9c4dc369febe70019586f3d570.jpg
[alt] =>
[caption] =>
)
[dc4358c470c33f20206afc180a28ae5b] => stdClass Object
(
[filename] => dc4358c470c33f20206afc180a28ae5b.jpg
[alt] =>
[caption] =>
)
)
那个stdobject让我很困惑。
更新。
在视图中我写这个:
foreach ($products as $prod)
{
echo $prod->prod_name.' - '.$prod->prod_slug.' - '.$prod->cat_slug.'<br>';
}
成功显示我想要的东西;
书柜木 - 书柜 - 木 - 存储
全新衣架Jhonson - 全新衣架 - jhonson - 衣架
闪木 - 闪木 - 商品
Gantungan baju dari kayu - gantungan-baju-dari-kayu - hanger
萨满灯 - 萨满灯 - 灯
储物木架 - 储物架 - 储物架
木灯 - 木灯 - 灯
木灯超越 - 木灯超越 - 灯
Yoyo Kayu - boneka-kayu-lucu - 商品
它仍然是json格式的图像,我不知道如何操作它,我只是想着array_value,将解码后的json_data转换为数组,也许?
答案 0 :(得分:0)
您是否有令人信服的理由使用JSON数据?如果没有,只需创建一个名为tbl_products_images的新表,并使用之前的JOIN加入它。
答案 1 :(得分:0)
如果您想要将第一张图片或primary
设置为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>';
}
循环遍历整个列表,将第一个图像设置为最终结果。如果您发现primary
设置为true
的图片,则替换$finalImage
并退出循环。
您可以将其置于接受images
作为参数的函数中,并返回$finalImage
作为结果。