0

I'm newbie in PHP and Opencart and I want to show a list of products with their appropriate categories, but I don't know how to manage it with array:

$data['heading_title'] = $this->language->get('heading_title');

$results = $this->model_catalog_profile->getProducts();

if ($results) {
    foreach ($results as $result) {

        // Categories
        $categories = $this->model_profile_profile->getProductCategories($result['product_id']);

        $data['product_categories'] = array();

        foreach ($categories as $category_id) {
            $category_info = $this->model_profile_category->getCategory($category_id);
            if ($category_info) {
                $data['product_categories'][] = array(
                    'category_id' => $category_info['category_id'],
                    'name' => $category_info['name']
                );
            }
        }


        $data['products'][] = array(
            'product_id'  => $result['product_id'],
            'thumb'       => $image,
            'name'        => $result['name'],
            'product_categories'    => $data['product_categories'][]
            )
        );

    }

    return $this->load->view('module/latest', $data);
}

And here is the code view:

<?php foreach ($products as $product) { ?>
<div class="profile-thumb transition">
    <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" /></a></div>
    <div class="caption">
        <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>
        <p><?php echo $product['description']; ?></p>

        <div id="product-category" class="" style="height: 120px; overflow: auto;">
            <?php foreach ($product_categories as $product_category) { ?>
">
7
  • Try this array_push($data['products'], $new_arr); Commented May 18, 2016 at 6:57
  • @FrayneKonok, I'm sorry, I really didn't understand what to do or where to use this code! Commented May 18, 2016 at 6:59
  • instead of $data['products'][] = array(....) use array_push($data['products'], $new_arr); Commented May 18, 2016 at 7:01
  • @FrayneKonok, Would you please post an Answer The Question? I really don't understand. array_push($data['products'], $new_arr) = array( 'product_id' => $result['product_id'], 'thumb' => $image, 'name' => $result['name'], WHERE SHOULD I ADD CATEGORIES? ) ); Commented May 18, 2016 at 7:06
  • 1
    Just remove [] from 'product_categories' => $data['product_categories'][]. It should be 'product_categories' => $data['product_categories'] Commented May 18, 2016 at 7:41

2 Answers 2

1

AS per the discussion, Just remove [] from 'product_categories' => $data['product_categories'][]. It should be 'product_categories' => $data['product_categories']

Updated Portion:

$data['products'][] = array(
    'product_id'  => $result['product_id'],
    'thumb'       => $image,
    'name'        => $result['name'],
    'product_categories'    => $data['product_categories']
    )
);

This may be worked for you.

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

Comments

1

You're on the right track here, although I think this might be more appropriate to what you're trying to achieve:

if ($results) {

  // For best practice, let's define the products key value first
  $data['products'] = array();

  foreach ($results as $result)
  {
    // Create the array for our product
    $product = array(
      'product_id'         => $result['product_id'],
      'thumb'              => $image,
      'name'               => $result['name'],
      'product_categories' => array()
    );

    // Fetch the categories for this product
    $categories = $this->model_profile_profile->getProductCategories($result['product_id']);

    foreach ($categories as $category_id)
    {
        $category_info = $this->model_profile_category->getCategory($category_id);
        if (!$category_info) continue;

        // Assign our category information a new category array
        $category = [
          'category_id' => $category_info['category_id'],
          'name'        => $category_info['name']
        ];

        // Push the new category array to our products array
        array_push($product['product_categories'], $category);

        // Optionally, free up some memory
        unset($category_info);
    }

    // Now push our new product array to our data array
    array_push($data['products'], $product);

    // Optionally, we can perform some clean up
    unset($categories);
}

return $this->load->view('module/latest', $data);

What we've done here is define our $data['products'] array first - this just makes the code much easier to read and quickly scan through.

Then, we've focused on creating a $product array before worrying about categories; that way we can create our product_categories key within the array, with a blank array as the initial value.

Next, after we've fetched the categories using getProductCategories, we enumerate through the returned array, creating a new $category array along the way, and then using array_push to add it to our $product['product_categories'] array.

Lastly for the loop, after we've fully constructed our new $product array, we use array_push again to add it to our $data['products'] array.

Hopefully this helps.

1 Comment

Thanks! I used "'product_category' => $cats," and it fixed the problem.

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.