Jump to content

Add row via API to a nested repeater


taotoo
 Share

Recommended Posts

I have a repeater that outputs a list of names, and a form so that people can add their name to the list:

<?php echo $page->$title; ?>

	<?php foreach($page->repeater as $repeat): ?>

		<?php echo $repeat->text; ?>

	<?php endforeach; ?>

<form action="/" method="post">
	<label for="repeat">Name:</label>
    <input type="text" name="repeat">
    <button type="submit">Submit</button>
</form>

e.g.

Monday
Ann
Ben
Charlie

Name: [          ]
[Submit]

At the top of the page I have this to process the form:

<?php
	if ($input->post->repeat) {
      $repeat = $page->repeater->getNew();
      $page->of(false);
      $repeat->text = $input->post->repeat;
      $page->repeater->add($repeat);
      $page->save();
	}
?>

Now I want to have the same, but for every day of the week, all on one page, like this:

Monday
Ann
Ben
Charlie

Name: [          ]
[Submit]

Tuesday
Darren
Ben
Fred

Name: [          ]
[Submit]

...

So I've nested a repeater like this:

<?php foreach($page->repeater as $repeat): ?>

	<?php echo $repeat->text; ?>

		<?php foreach($repeat->repeater2 as $repeat2): ?>

			<?php echo $repeat2->text; ?>

		<?php endforeach; ?>

	<form action="/" method="post">
		<label for="repeat2">Name:</label>
		<input type="text" name="repeat2">
		<button type="submit">Submit</button>
	</form>

<?php endforeach; ?>

And updated the other code like this:

<?php
	if ($input->post->repeat2) {
      $repeat2 = $page->repeater->repeater2->getNew();
      $page->of(false);
      $repeat2->text = $input->post->repeat2;
      $page->repeater->repeater2->add($repeat2);
      $page->save();
	}
?>

But when I try to add a new name using the form, I get this error:

Call to a member function getNew() on null

..on this line:

 $repeat2 = $page->repeater->repeater2->getNew();

I guess I need to tell "getNew" which of the nested repeaters I want the new row to be appended to.

I thought maybe I could add a second field to the form, something like:

<input type="text" name="repeaterRowID" value="<?php echo $repeat; ?>">

(The outer repeater row ID seems to be similar to the inner repeater ID)

And then change the "getNew" line to something like:

$repeat2 = $pages->repeater("id=($input->post->repeaterRowID)")->repeater2->getNew();

But can't figure it out...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...