# Load more causes error

**URL:** <https://forum.getkirby.com/t/load-more-causes-error/7965>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 13, 2017, 12:51pm UTC](https://forum.getkirby.com/t/load-more-causes-error/7965 "2017-07-13T12:51:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pppws](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pppws/32/3687_2.png) [@pppws](https://forum.getkirby.com/u/pppws)\
**Post date:** [July 13, 2017, 12:51pm UTC](https://forum.getkirby.com/t/load-more-causes-error/7965/1 "2017-07-13T12:51:26Z")

</div>

hey there,

i’m trying to use the load more ([https://getkirby.com/docs/cookbook/ajax-load-more](https://getkirby.com/docs/cookbook/ajax-load-more)) function for my blog articles.

the first problem i encounter is the following php error:  
Undefined variable: limit  
in this line:  
`<div class="articles" data-page="<?= $page->url() ?>" data-limit="<?= $limit ?>">`

after replacing the $limit with 2 i encounter an other error which was already solved in the forum ([Load More Ajax Problems](https://forum.getkirby.com/t/load-more-ajax-problems/7238)) by switching to mamp – but i’m already using mamp and i can’t figure out my mistake. the console error:  
`GET http://localhost:8888/dustmann/news.json?limit=2&offset=2 404 (Not Found)`

news.php:

```auto
    <main class="main <?= $page->template() ?>" role="main">
        <h1><?= $page->title() ?></h1>
        <div class="articles" data-page="<?= $page->url() ?>" data-limit="<?= $limit ?>">

            <?php foreach($page->children()->visible()->flip() as $article): ?>
                <?php snippet('article', compact('article')) ?>
            <?php endforeach ?>
        </div>
        <button class="load-more">More news</button>
    </main>

```

articles controler

```auto
    <?php
    return function($site, $pages, $page) {
      $articles = $page->children()->visible();
      $count = $articles->count();
      // check if the request is an Ajax request and if the limit and offset keys are set
      if(r::ajax() && get('offset') && get('limit')) {
        // convert limit and offset values to integer
        $offset = intval(get('offset'));
        $limit = intval(get('limit'));
        // limit articles using offset and limit values
        $articles = $articles->offset($offset)->limit($limit);
        // check if there are more articles left
        $more = $count > $offset + 1;
      // otherwise set the number of articles initially displayed
      } else {
        $offset = 0;
        $limit = 2;
        $articles = $articles->limit($limit);
      }
      return compact('offset', 'limit', 'articles', 'more');
    };

```

articles.json.php

```auto
        <?php
            $html = '';
            foreach($articles as $article) {
              // reuse the article snippet to create the HTML for each article
              // we need to set the third parameter to true, to return the
              // snippet content instead of echoing it
              $html .= snippet('article', compact('article'), true);
            }
            // add $html and $more to the $data array
            $data['html'] = $html;
            $data['more'] = $more;
            // JSON encode the $data array
            echo json_encode($data);

```

---

<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:** [July 13, 2017, 1:15pm UTC](https://forum.getkirby.com/t/load-more-causes-error/7965/2 "2017-07-13T13:15:02Z")

</div>

Looks like you are using a news.php template, but an articles.php controller, that would explain why the `$limit` variable is unknown to the template and the rest doesn’t work either.

---

<div class="post-metadata">

**Author:** ![pppws](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pppws/32/3687_2.png) [@pppws](https://forum.getkirby.com/u/pppws)\
**Post date:** [July 13, 2017, 2:48pm UTC](https://forum.getkirby.com/t/load-more-causes-error/7965/3 "2017-07-13T14:48:14Z")

</div>

i renamed the template to articles.php – which fixed the php error concerning the `$limit` viariable.

but the console still returns an error:  
GET [http://localhost:8888/dustmann/undefined.json?limit=NaN&offset=NaN](http://localhost:8888/dustmann/undefined.json?limit=NaN&offset=NaN) 404 (Not Found)

the NaN means that there is stell an error with the `$limit` variable?

---

<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:** [July 13, 2017, 3:27pm UTC](https://forum.getkirby.com/t/load-more-causes-error/7965/4 "2017-07-13T15:27:59Z")

</div>

Could you post your javascript, please? Most likely, you are not grabbing the correct element…

---

<div class="post-metadata">

**Author:** ![pppws](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pppws/32/3687_2.png) [@pppws](https://forum.getkirby.com/u/pppws)\
**Post date:** [July 13, 2017, 3:47pm UTC](https://forum.getkirby.com/t/load-more-causes-error/7965/5 "2017-07-13T15:47:37Z")

</div>

ahhh! thanks for the tip…  
i was using the `.articles` twice.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:35pm UTC](https://forum.getkirby.com/t/load-more-causes-error/7965/6 "2025-02-06T17:35:21Z")

</div>

Archived on February 6, 2025, 5:35pm UTC


