Jump to content

Repeater returns number instead of values


jsantari
 Share

Recommended Posts

Hi 2nd day into PW and I think its great! I've setup a template with a repeater for widgets (things I want to load into the page). I've setup a field called widget_group_name which is type page with a radio input so I can pick one of my widgets. I also have another field in the group called widget_group_position which is just a text input.

There are two items in the group on the current page.

I use this code to access the repeater field and here is what comes back.

$group = $page->widget_group;
 foreach ($group as $item) {
   echo $item->widget_group_name.'<br>';
 }      
 
I get this output for widget_group_name:

1012|1013


1012         
 
It works correctly if I use widget_group_position in the loop above and displays the text value.              
 
 
Link to comment
Share on other sites

$item->widget_group_name->title (or whatever you need to output.)

echoing a page field will always return the ID

Either that or "echo $item->widget_group_name->render()", depending on where you want to specify markup for your widgets :)

Link to comment
Share on other sites

@teppo - thanks; i'm still not super hip to this whole render thing and i've searched the docs, cheatsheet and forum.. any chance you could provide a quick explanation of how render() works?

cheers

Link to comment
Share on other sites

@teppo - thanks; i'm still not super hip to this whole render thing and i've searched the docs, cheatsheet and forum.. any chance you could provide a quick explanation of how render() works?

cheers

This thread and this wiki page might help with explaining render(); 

render method outputs pages using their own template file

Hope this helps :)

  • Like 2
Link to comment
Share on other sites

Ok, then if I understand this I need to get the page of the widget using the id. Tried that but it doesn't work for me?

$group = $page->widget_group;
  foreach ($group as $item) {
      echo $item->widget_group_name.'<br>'; // this works as I see the id#
      $widget_page = $pages->get($item->widget_group_name).'<br>'; // get the page using the id - doesn't appear to work?
      echo $widget_page->block.'<br>'; // Shows me nothing - (block is the name of a field on the widget page)
}
Link to comment
Share on other sites

@kongondo thank you - i'll check those out

@jsantari - something like this should work ;

<?php

$group = $page->widget_group; // widget group is a repeater

  foreach ($group as $item) { // each repeater item has 2 fields, one is a page select called widget_group_name ?>
     
     <ul>
     	<li><?php echo $item->widget_group_name->title; ?></li>
      	<li><?php echo $item->widget_group_name->block; ?></li>
     </ul>

<?php } // end foreach ?>

Also want to mention that the field names are not really optimal; for example the page select should probably be just widget

Link to comment
Share on other sites

Tried your code but get no output at all in the list.

Why is the using just widget 'optimal'?

to me it doesn't seem optional since it's not really the 'widget group name' but is the actual page that is holding the widget, and it's also shorter; unless i have a misunderstanding of your fields and templates names; 

if you can post the names of all relevant fields and templates including the repeater field, and what fields are inside the repeater i might be able to do better...

but from what i see from your post this should work..

can you do a  

print_r ($group);

to see whats in that array?

maybe also the same for the $item?

Link to comment
Share on other sites

$group = $page->widget_group;
foreach ($group as $item) {
      echo $item->widget_group_name.'<br>'; // this works as I see the id#
      $widget_page = $pages->get($item->widget_group_name).'<br>'; // get the page using the id - doesn't appear to work?
      echo $widget_page->block.'<br>'; // Shows me nothing - (block is the name of a field on the widget page)
}
 

if "widget_group_name" is a page field you don't need to get the page, as you already got it!

Though if that page field is set to multiple (PageArray) it would be always an array even if only one page selected in the field.

So you'd have to threat it as PageArray and use first to get the first page selected.

echo $item->widget_group_name->first->title

Or if you change the page field to single page you can use 

echo $item->widget_group_name->title
  • Like 1
Link to comment
Share on other sites

As Soma points out, the issue must be that you have specified to allow multiple pages to be selected, which you shouldn't because you're using the repeaters to handle the adding of widgets, and you have an additional field in the repeater item related to positioning the widget. So it makes sense to limit that page select to 1 page;

  • Like 1
Link to comment
Share on other sites

Ok I've simplified things as suggested. I've added two repeater (widget) items to my page. Tried some more code and still have several problems. First one is that this code only retrieves the first item. My count comes back as 1 not 2.

$group = $page->widget_group;
echo 'There are [ '.count($group).' ] widgets assigned<hr>';

I also have problems with getting the right data back inside my loop. Here is that code.

foreach ($group as $item) {
    echo 'Widget Title: '.$item->widget_group_name->title.'<br>'; // OK
    echo 'Widget block content: '.$item->widget_group_name->block.'<br>'; // OK
    echo 'Widget access: '.$item->on_device.'<br>'; // WRONG DATA RETURNED
    echo 'Placement position: '.$item->widget_group_position.'<hr>'; // WRONG DATA RETURNED
}

Here is the output:

Widget Title: My First Block
Widget block content: This is my first widget block content
Widget access: 8
Placement position: left

Widget Title and Widget block content are correct.

Widget access returns a number 8, that input in the repeater is a select list that I created and the value that is currently selected should be 16.

Placement position is a text input in the repeater with a current value of 'sidebar', as you can see it shows 'left'.

No idea where it is getting either of these values from.

Having a hard time getting my head around how PW does things which is frustrating but I can see the great potential that it has.

Link to comment
Share on other sites

what about this, just taking a guess here, that you need to count the actual repeaters before assigning them to a variable?

<?php
echo 'There are [ '. count($page->widget_group) . ' ] widgets assigned<hr>';
?>

also can you try

foreach ($page->widget_group as $item) {

because you may be encountering some strangeness by trying to assign what is already a page array into a new variable.
 

Link to comment
Share on other sites

If you had a page field with allowing multiple pages (PageArray) and changed it to allow single page later, there could be multiple still saved to the field but you only see one. Try changing it back to multiple pages and check if there isn't more than 1 selected in those on the page.

Link to comment
Share on other sites

Was looking at the data in the db and realized what I was overlooking so now I can admit to making a dumb mistake in my testing  :rolleyes: - I moved my code to a testing only page template and forgot that I was now referencing from a different page! Classic couldn't see the forest for the trees. It's working as expected and I must admit is slick.

thanks to all for their feedback.

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...