# Create page in loop creates only one page at a time

**URL:** <https://forum.getkirby.com/t/create-page-in-loop-creates-only-one-page-at-a-time/27509>\
**Category:** Questions\
**Tags:** v3\
**Created:** [January 11, 2023, 10:24am UTC](https://forum.getkirby.com/t/create-page-in-loop-creates-only-one-page-at-a-time/27509 "2023-01-11T10:24:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [January 11, 2023, 10:24am UTC](https://forum.getkirby.com/t/create-page-in-loop-creates-only-one-page-at-a-time/27509/1 "2023-01-11T10:24:46Z")

</div>

hi!

i am sending an array of data through an input form to the backend and have setup a page controller to handle it:

the code works, except i am looping the array and need to create a subpage for each item in the array and only one page is created.

here the code in the controller:

```auto
// check if request is POST and input type submit has been clicked
if ($kirby->request()->is('POST') && get('post_comment')) {

    // check if the honeypot trap has been triggered
    if (empty(get('website')) === false) {
        go($page->url());
        exit;
    }

    // we POST a list of fields all named the same
    // `comment_data[]`, the `[]` put the fields inside
    // a common array.
    // let's loop over the array and process each field,
    // which is JSON stringify value

    $body = get('comment_data');
    // file_put_contents('log.txt', json_encode($body));
    
    foreach($body as $field) {

        // get the data and check if it's all good

        $field = json_decode($field, true);
        $selection_text = yaml::encode($field['content']['selection_text'], true);

        $content = $field['content'];
        $data = [
            'user' => $content['user'],
            'timestamp' => $content['timestamp'],
            'article_slug' => $content['article_slug'],
            'block_id' => $content['block_id'],
            'text' => $content['text'],
            'selection_type' => $content['selection_type'],
            'selection_text' => $selection_text
        ];

        $rules = [
            'user' => ['required'],
            'text' => ['required'],
        ];

        $messages = [
            'user' => 'Please set a name',
            'text' => 'Please write a comment',
        ];

        // if data has some problems stop everything
        // and refresh page
        if ($invalid = invalid($data, $rules, $messages)) {
            go($page->url());
            exit;

        } else {
            // else proceed with requested operation:
            // - act as the API user
            // - create subpage for comment

            $user = $kirby->users()->filterBy('role', '==', 'api')->first();
            $kirby->impersonate($user->email());

            try {

                // make subpage under <current-article>/comments>
                $comments_subpage = $page->find('comments');
            
                $comment = $comments_subpage->createChild([
                    'slug' => $content['timestamp'],
                    'template' => $field['template'],
                    'content' => $data
                ]);

                // => done!
                // maybe show an <article>/comment-added subpage
                // explaining what happened?
                go($page->url());

            } catch (Exception $e) {
                $alert = ['Adding your comments failed: ' . $e->getMessage()];
            }
        }

    } // -- ends data loop

}

```

if i send the same form one more time from the frontend, the next item in the list will be created as a subpage, etc. i browsed the forum and the docs and can’t find which silly mistake i am making (🙂 … any tip appreciated.

thanks!

---

<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:** [January 11, 2023, 11:54am UTC](https://forum.getkirby.com/t/create-page-in-loop-creates-only-one-page-at-a-time/27509/2 "2023-01-11T11:54:41Z")

</div>

> [@afincato](#):
>
> ` go($page->url());`

This piece of code after creating the page redirects after the first round in the loop. Redirecting should happen outside the loop. Also, store your alerts in an array.

---

<div class="post-metadata">

**Author:** ![afincato](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/afincato/32/3446_2.png) [@afincato](https://forum.getkirby.com/u/afincato)\
**Post date:** [January 11, 2023, 12:15pm UTC](https://forum.getkirby.com/t/create-page-in-loop-creates-only-one-page-at-a-time/27509/3 "2023-01-11T12:15:23Z")

</div>

dang! noob.

thank you!
