This is an example of my table product and category in database:
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
where the content of images is json_encoded like this one:
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"
}
}
etc.
First thing I want is to get every product to be shown on my ecommerce product page, so in my controller products:
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);
}
in my 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;
}
}
How to display product name, prod_slug, cat_slug and only one images['filename]
for every product in my view, and if "primary = true" than show the primary image else show the first images. example in array maybe images[0];
.
I have checked the data from fields images with this code:
foreach ($products as $prod)
{
$prod->images = json_decode($prod->images);
print_r($prod->images);
}
and that shows stdClass
Object like this:
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] =>
)
)
That stdobject makes me confused.
update.
in view i write this:
foreach ($products as $prod)
{
echo $prod->prod_name.' - '.$prod->prod_slug.' - '.$prod->cat_slug.'<br>';
}
it success display what i want;
Book Cabinets Wood - book-cabinets-wood - storage
Brand New Hanger Jhonson - brand-new-hanger-jhonson - hanger
Flash Wood - flash-wood - merchandise
Gantungan baju dari kayu - gantungan-baju-dari-kayu - hanger
Shaman Lamp - shaman-lamp - lamp
Storage Wood Shelf - storage-wood-shelf - storage
Wood Lamp - wood-lamp - lamp
Wood Lamp Transcending - wood-lamp-transcending - lamp
Yoyo Kayu - boneka-kayu-lucu - merchandise
the images it still in json format, and i dont know how to manipulate it, im just thinking about array_value, to convert json_data after decoded to array, maybe?