0

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?

4

2 回答 2

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 作为结果返回的函数中。

于 2013-07-01T07:51:57.427 回答
0

您有使用 JSON 数据的令人信服的理由吗?如果没有,只需创建一个名为 tbl_products_images 的新表并将其与之前的 JOIN 一起加入。

于 2013-07-01T07:11:55.700 回答