Skip to content

Instantly share code, notes, and snippets.

@kongondo
Forked from somatonic/repeater_example.php
Created June 7, 2013 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kongondo/5728079 to your computer and use it in GitHub Desktop.
Save kongondo/5728079 to your computer and use it in GitHub Desktop.
example repeater creation and save front-end form
<?php
$mypage = $pages->get("/about/");
if($input->post->submit){
$n = 1;
$title = "element_title_$n";
$url = "external_url_$n";
$mypage->setOutputFormatting(false);
while($input->post->$title){
// on first round remove all repeater items, to keep things easy.
// teasers is the repeater field
if($n == 1) $mypage->teasers->removeAll();
// create new repeater item
$item = $mypage->teasers->getNewItem();
// populate any fields in the repeater you want
$item->element_title = $input->post->$title;
$item->external_url = $input->post->$url;
// save the repeater item (a page itself)
$item->save();
// add the repeater item to the page
$mypage->teasers->add($item);
// update counter and field names
$n++;
$title = "element_title_$n";
$url = "external_url_$n";
}
if($n > 1) {
// save the page
$mypage->save('teasers');
}
}
?>
<form name="test" method="post">
<?php $count = 1; foreach($mypage->teasers as $key => $t): ?>
<input type="text" name="element_title_<?php echo $key+1?>" value="<?php echo $t->element_title?>"/>
<input type="text" name="external_url_<?php echo $key+1?>" value="<?php echo $t->external_url?>"/><br/>
<?php $count++; endforeach; ?>
<input type="text" name="element_title_<?php echo $count; ?>" value=""/>
<input type="text" name="external_url_<?php echo $count; ?>" value=""/><br/>
<input type="text" name="element_title_<?php $count++; echo $count; ?>" value=""/>
<input type="text" name="external_url_<?php echo $count; ?>" value=""/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment