Jump to content

How to output the selected field items in a Repeater field?


gtoirog
 Share

Recommended Posts

Hi,

I have three fields: Select, Checkbox and Checkbox. I added these fields into a Repeater field and I can see the fields in Admin and I can select from the Select, tick the Checkboxes. But I don't know how to output the actual selected values. I read that the Repeater field contains my values as pages. But not sure, how to display. It seems like pages inside pages? :undecided:

Link to comment
Share on other sites

EDIT: wrong. don't follow.

as an example:

<?php echo "{$page->myRepeater->myField}"; ?>

as you have a checkbox, you will want to evaluate if your checkbox is checked or not. example:

<?php if($page->myRepeater->myCheckbox): ?>

// do something

<?php else: ?>

// do something different

<?php endif; ?>
Link to comment
Share on other sites

Thanks for replying. All my fields in the Repeater field are Page fields with Select, Checkbox input types. So I still can not understand how I can print the title of a Page selected in my Select field inside a Repeater field and titles of checked values (Pages) in the same Repeater? It's my third day being with ProcessWire and am weak at coding, doh

Link to comment
Share on other sites

i'm not quite sure if i understand right where you are heading for. however, if you have selected "checkboxes" under "input field type" for a page field, than the field returns an array. you'll need to foreach loop it in order to get the value of each title:

<?php foreach($page->myRepeater as $repeaterItem): ?>

<h1><?php echo "{$repeaterItem->title}"; ?></h1>

<?php endforeach; ?>

but why do you have grouped several page fields in a repeater?

  • Like 1
Link to comment
Share on other sites

I don't get any output from the codes suggested and tried :( My website is about plants, so for each plant, I need to select values for different purposes. I have a page called Plants and under that all the plants as children. For each plant, I need to select values from a list or checkboxes. For example, for a plant called "Pine tree", 
i need to select "Which parts to use" as a Select input type with values "trunk, root, bark, branch"; and check values from a checkbox called "Type of product" with values like "table, chair, box etc". It's just an example. All these values are pages under their corresponding parent pages like "Parts" and "Products" under "Home". I need to repeat these fields for each part. For example, I need to select first "trunk" and then check "table, chair". In my second repeat, I will select "root" and then checkboxes "chair, box". And then next maybe "bark" for "table, box" et cetera. Does that make any sense? Am I doing it nonsense way or?

Link to comment
Share on other sites

hm, i'm not an expert myself and not a native englisch speaker but from my understanding this sounds like a category system as already often discussed here. as i understand it, i wouldn't wrap my page fields in a repeater, as repeaters are mainly used to give a "set" of fields, that you may repeat over and over again on your page. a repeater would make sense if, for example, you would need the category "which parts to use" twice on the page "pine tree".

if i don't understand you completely wrong, i would attach several page fields to my templates each of one serving for one category (e.g. one page field for "parts to use", one for "type" etc.). than i would access each field with an foreach loop as described above.

let me know if this works for you.

EDIT: the blog profile works this way. look how tags and categories are implemented there http://modules.processwire.com/modules/blog-profile/

Link to comment
Share on other sites

Hi gtoirog,

Welcome to PW and the forums! 

totoff is right; you don't want to use repeater fields to categorise content.

I don't know how much reading you've done (about PW). This thread would be useful for categorising content. May I also suggest, it is (at least for me) easier to understand what you are trying to achieve if it can be visualised. So, something like how you tree structure looks like and the relationships you are trying to take advantage of helps. Eg.

My tree looks like this:

Home

plants

Species

Color

Etc

Link to comment
Share on other sites

Home

    plants

         a

           a_plant1

           a_plant2

         b

           b_plant1

           b_plant2

         c...

    plant_parts 

         part_a

         part_b

         part_c

    products_made_fromplants

         product_a

         product_b

         product_c

on "a_plant1" page, I have a template with the repeater selector which contains the select box (page field type) with options "part_a, part_b, part_c" from "plant_parts" as pages, and checkbox (with values: product_a, product_b, product_c).

so if i select "part_a", i can tick "product_a and product_b",

if i select "part_b" then i can check "product_a, product_c" etc. on "a_plants1" page.

This is the best I could organize. I just can't read the values from my select box and checkbox within the repeater field.

If a repeater can contain these page fields then i should be able to read the values, shouldn't i?

Link to comment
Share on other sites

Let's do this. You should be able to read / output your values with something like this:

// Loop your plants
foreach ($pages->find("template=plant") as $plant) {
  // selected plant part
  echo $plant->plant_part->title;
  // selected products (one or more)
  if (count($plant->products)) {
    foreach ($plant->products as $p) {
        // Title of selected product
        echo $p->title;
      }
  }
}
Link to comment
Share on other sites

it was all nested pages. I got the result I want after 3 nested loops as following, but not sure if it is a right thing to do or not  for hundreds of records  :undecided:  i'll go read about how to use Repeaters properly now
 
foreach ($page->repeater as $repeater_item) {
    foreach ($repeater_item as $pages_selected) { 
        foreach ($pages_selected as $page_selected) {        
            echo $page_selected->title;
        }     
    }
}
 
Link to comment
Share on other sites

only two levels were necessary, i guess it's okay to use a repeater like i mentioned above?!

foreach ($page->repeater as $repeater_item) {
    $plant_parts = $repeater_item->get("plant_parts");
    foreach($plant_parts as $plant_part) {
        echo $plant_part->title;
    }
}
 
Link to comment
Share on other sites

If it works for you, well, and if there are no severe strains on your server and if you've thought this through, then, yes....But, as has been said before, repeaters were designed for something else and not to categorise content...Most people here will categorise content using methods stated above...it is good practice..:)

I'm sure you've seen this:

When not to use repeaters

Repeaters aren't infinitely scalable in quantity, so avoid repeaters for quantities of items that you think may need to be infinitely scalable. When you edit a repeater field in your page, you edit all items at once. As a result, you probably don't want to use a repeater if you expect hundreds of items, as that may slow your editing experience. It's better to use pages without repeaters when dealing with huge quantities of items. 

You also don't want to use repeaters for items of content that you want to have their own URL. Individual repeater items don't have individual URLs, so repeaters are better for items that you will be showing as a group on one page. However, there's nothing preventing you from attaching repeater items to URL segments or GET variables (for example), but consider whether you would be better off just using regular pages.

  • Like 1
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...