0

I'm working with an OpenCart store that's based on an mcv design pattern. I need have a model function that returns an array but I need to make a call within that array to another function that returns an array. This function below breaks here:

'gallery_images' => $this->getGalleryImages($data['product_id'])

public function getImportProductInfo() {
    $query = $this->db->query("SELECT `product_id` FROM " . DB_PREFIX . "product WHERE `status` = '0' AND `affiliate_id` = '" . $this->affiliate->getID() . "'");   

    $pids = array();
    foreach($query->rows as $result) {
        $pids[] = $result['product_id'];
    }

    // product & product description
    $query_product = $this->db->query("SELECT    p.model,
                                                 p.product_id,
                                                 p.quantity,
                                                 p.image,
                                                 p.price,
                                                 p.weight,
                                                 p.length,
                                                 p.width,
                                                 p.height,
                                                 pd.name AS 'product_name',
                                                 pd.description AS 'product_description',
                                                 cd.name AS `category_name`,
                                                 m.name AS 'manufacturer_name'
                                      FROM       " . DB_PREFIX . "product p
                                      LEFT JOIN  " . DB_PREFIX . "product_description pd ON p.product_id = pd.product_id
                                      LEFT JOIN  " . DB_PREFIX . "product_to_category ptc ON p.product_id = ptc.product_id
                                      LEFT JOIN  " . DB_PREFIX . "category_description cd ON ptc.category_id = cd.category_id
                                      LEFT JOIN  " . DB_PREFIX . "manufacturer m ON p.manufacturer_id = m.manufacturer_id
                                      WHERE      p.product_id IN (" . $this->db->escape(implode(',',$pids)) . ")");


    foreach($query_product->rows as $data) {
            $product_data[] = array(
              'product_id'           => $data['product_id'],
              'model'                => $data['model'],
              'quantity'             => $data['quantity'],
              'featured_image'       => $data['image'],
              'price'                => $data['price'],
              'weight'               => $data['weight'],
              'length'               => $data['length'],
              'width'                => $data['width'],
              'height'               => $data['height'],
              'product_name'         => $data['product_name'],
              'description'          => $data['product_description'],
              'category_name'        => $data['category_name'],
              'manufacturer_name'    => $data['manufacturer_name'],
              'gallery_images'       => array($this->getGalleryImages($data['product_id'])) 

            );      
    }

    return $product_data;
}

Here is the getGalleryImages() function. Note here I have also tried return implode(', ',$images); so i dont have to build the array but it still breaks.

public function getProductGalleryImages($product_id) {
    $query = $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$product_id . "'");

    $images = array();
    foreach($query->rows as $result) {
        $images[] = $result['image'];   
    }

    //return implode(', ',$images);
      return $images;
}

each product has multiple image urls stored in an intersecting table and I need to add to the array and it's been fighting me all morning... Any Ideas would be greatly appreciated. Thanks in advance :)

OK here is the sql embedded in the array that worked

'gallery_images' => $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$data['product_id'] . "'")

BUT it returns this:

[gallery_images] => stdClass Object ( 
[row] => Array ( [image] => motorcycle/12_06_27/031.JPG ) 
    [rows] => Array ( [0] => Array ( [image] => motorcycle/12_06_27/031.JPG ) )[num_rows] => 1 ) ) )

All I want is the ['image'] part.

1 Answer 1

3

Just omit the "array" part. Instead of

array($this->getGalleryImages($data['product_id'])) 

use

$this->getGalleryImages($data['product_id']) 

Since (and if) this function will return an array, this is the correct syntax. If you want to be safe about the returning value to be an array (that is, force it to be), you can do this:

array() $this->getGalleryImages($data['product_id'])

...which is ALMOST the same thing but notice that array() bracket is immediately closed. It means you "typecast" the result to an array. (I wonder if this was your original intention btw...?)

You can use arrays in arrays, there's no problem with that. Now if this is not the right answer, let's clarify the question itself :)

Sign up to request clarification or add additional context in comments.

9 Comments

@dkelliner So, your answers are correct towards my question but I have already tried all of those scenarios and they still break which is why I'm confused. If I embed the sql directly within the array it works but It returns a bunch of stuff I don't need also. I have added to the code above to show you my results.
Tell me it's not "getProductGalleryImages" vs "getGalleryImages" :)
(Also, you're doing a query inside a query which is not very healthy for your speed. There are other ways. But that's another question.)
What does it mean "it breaks", btw? Is it some error message or just an empty result?
That must be an error, it just gets redirected to some logfile. Can you access that? We need the error message if there is one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.