# Problem with categories

**URL:** https://forum.getkirby.com/t/problem-with-categories/15320
**Category:** Questions
**Tags:** v3
**Created:** [August 20, 2019, 8:57am UTC](https://forum.getkirby.com/t/problem-with-categories/15320 "2019-08-20T08:57:20Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 8:57am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/1 "2019-08-20T08:57:20Z")

</div>

Trying to get the children of a page as categories but it just throws me the error „Undefined variable: products”. Can’t find the mistake i’ve done …

The structure is: Parent → Children (Categorie) → Grandchildren (Articles).

**Controller**

```
<?php

return function ($site, $pages, $page) {

	$categories = $page->children()->visible();
	$products = $page->grandChildren()->visible();

	if ($categories->count() == 0 and $products->count() == 1) {
		go($products->first()->url());
	}

	return [
		'categories' => $categories,
		'products' => $products,
	];

};

```

**Template**

```
<?php if (page('shop')->children()->count() > 0): ?>
  <div>
    <?php snippet('treemenu',array('subpages' => page('shop')->children())) ?>
  </div>
<?php endif ?>

<ul>
  <?php foreach($page->grandChildren() as $product): ?>
  <li>
    <a href="<?= $product->url() ?>">
      <?= html($product->title()) ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 20, 2019, 9:04am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/2 "2019-08-20T09:04:37Z")

</div>

Can there be any grandChildren if there are no children? I’m not fully awake yet, so my brain doesn’t work properly yet, but somehow the logic of

```auto
if ($categories->count() == 0 and $products->count() == 1) {
		go($products->first()->url());
	}

```

is not quite clear to me.

But apart from that, the Whoops screen should tell you in what line and file the error is thrown?

On a side note: If you define your `$products` variable in the controller, it would make sense to use it in the template, instead of calling `$page->grandChildren()` again.

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 9:28am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/3 "2019-08-20T09:28:29Z")

</div>

You’re right, it does not make sense 😅

Actually this part:

```
<ul>
  <?php foreach($page->grandChildren() as $product): ?>
  <li>
    <a href="<?= $product->url() ?>">
      <?= html($product->title()) ?>
    </a>
  </li>
  <?php endforeach ?>
</ul>

```

is coming from a snippet and there is the **error** on the first line:

```
<?php if(count($products) or $products->count()): ?>
<section>
   <?php foreach($products as $product): ?>
   <div>
      <a href="<?php echo $product->url() ?>">
         <div>
            <?php echo $product->title()->html() ?>
         </div>
      </a>
   </div>
   <?php endforeach ?>
</section>
<?php endif ?>
```

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 9:35am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/4 "2019-08-20T09:35:09Z")

</div>

If you call a snippet, you have to pass the variable to it.

But why `<?php if(count($products) or $products->count()): ?>`?

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 9:50am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/5 "2019-08-20T09:50:00Z")

</div>

If there are no products i want to show a message with an if else statement.

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 9:51am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/6 "2019-08-20T09:51:46Z")

</div>

Sure, but

```auto
<?php if ($products->count()): ?>

```

would be fully sufficient.

Or you can also use

```auto
<?php if ($products->isNotEmpty()): ?>

```

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 10:07am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/7 "2019-08-20T10:07:54Z")

</div>

Thanks.

But the actual problem isn’t fixed yet.

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 10:12am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/8 "2019-08-20T10:12:41Z")

</div>

Could you again please post template, controller and snippet including that part where the snippet is used.

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 10:23am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/9 "2019-08-20T10:23:06Z")

</div>

Sure, here the actual code:

**Template** category.php and shop.php (exactly the same)

```
<?php if (page('shop')->children()->count() > 0): ?>
    <div>
      <?php snippet('treemenu',array('subpages' => page('shop')->children())) ?>
    </div>
<?php endif ?>

<?php snippet('product.list') ?>

```

**Snippet** product.list.php

```
<?php if ($products->count()): ?>
  <section>
    <?php foreach($products as $product): ?>
      <a href="<?php echo $product->url() ?>">
        <?php echo $product->title()->html() ?>
      </a>
    <?php endforeach ?>
  </section>
  <?php else: ?>
  <h1>No products</h1>
<?php endif ?>

```

**Controller**

```
<?php

return function ($site, $pages, $page) {

	$categories = $page->children()->visible();
	$products = $page->grandChildren()->visible();

	if ($categories->count() == 0 and $products->count() == 1) {
		go($products->first()->url());
	}

	return [
		'categories' => $categories,
		'products' => $products,
	];

};
```

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 11:14am UTC](https://forum.getkirby.com/t/problem-with-categories/15320/10 "2019-08-20T11:14:13Z")

</div>

As I already mentioned above, you have to pass the variable from the template to the snippet:

```auto
<?php snippet('product.list', ['products' => $products]) ?>

```

I’d remove this from the controller (as I don’t see the logic behind it)

```auto
if ($categories->count() == 0 and $products->count() == 1) {
		go($products->first()->url());
	}

```

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 12:15pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/11 "2019-08-20T12:15:39Z")

</div>

I’ve already tested to pass the variable to the snippet but i get the same error. (without your typo 😜)

**Undefined variable: products**

![19](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/2/28b38af47a873ac4eff9bff372d74c485fafe753.png)

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 12:23pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/12 "2019-08-20T12:23:08Z")

</div>

Do you have two controllers then for your two templates where this variable is defined?

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 12:30pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/13 "2019-08-20T12:30:09Z")

</div>

> Do you have two controllers then for your two templates where this variable is defined?

Of course not 🤦‍♂️

I’ve copied and renamed the shop controller to category, but it only shows me the else statement now.

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 12:33pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/14 "2019-08-20T12:33:44Z")

</div>

If it shows no projects then there either are none, or your reference is wrong, $page always refers to the current page…

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 12:40pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/15 "2019-08-20T12:40:07Z")

</div>

It shows me only the message „no projects“ … but what is wrong with the reference?

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 12:46pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/16 "2019-08-20T12:46:36Z")

</div>

Wrong filenames? Wrong page? I don’t know your structure…

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 12:59pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/17 "2019-08-20T12:59:21Z")

</div>

I’ve controlled all the names and the status of my products, but can’t find any mistake …

Structure: shop (parent) -\> category (child) -\> product. The categories are shown as links in the header.

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 1:02pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/18 "2019-08-20T13:02:19Z")

</div>

You can send me your project and I’ll take a look.

---

<div class="post-metadata">

### Author: ![klixx](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/klixx/32/7339_2.png) [@klixx](https://forum.getkirby.com/u/klixx)
#### Post date: [August 20, 2019, 1:14pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/19 "2019-08-20T13:14:12Z")

</div>

Maybe this helps:

![21](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/b/b9d24072f1bbc26bb6022eb96d7034896bae5e13.png)

Nice offer. I will despair soon 😄 Thank you!

---

<div class="post-metadata">

### Author: ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)
#### Post date: [August 20, 2019, 1:30pm UTC](https://forum.getkirby.com/t/problem-with-categories/15320/20 "2019-08-20T13:30:40Z")

</div>

Ok, in that shop folder there is a `shop.txt` file, right? And then you have a `shop.php` template and a `shop.php` controller? And inside the `shop.php` template, you call the `shop-list.php` snippet?

Ah, you are calling `visible` (that’s deprecated, anyway) pages, but your folders are not “visible”, they are published, but not listed.

[Next page](https://forum.getkirby.com/t/problem-with-categories/15320.md?page=2)
