Jump to content

Getting field name instead of field value


test123
 Share

Recommended Posts

Hi everyone,

I am new to ProcessWire and have an question about the methods get field values.

First of all my page setup:

I have a page as a child of the Admin page which is named Data. The idea is to create all my data objects as children of this page. So the data page and all of its children should never be seen on the front-end. Every page uses the same template. The template has one field named playername of type Text.

Now I am looping through all pages which are children of the Data page in this way (The page id is 1011):

//get the data-page
    $datapage = $pages->get(1011);
    
    //get all children from data-page
    $players = $datapage->children();
    
    //loop through the children
    foreach($players as $player) {
        $field = $player->fields->get("playername");
        echo $player->title . " " . $field . "<br> ";
    }

The problem now is, that I don't get the value of the field playername. Instead the output of $field is "playername". Why am I getting the field name instead of the fieldvalue. I am expecting to get the string, which is entered in the playername-field and not the name.

Hope anybody can help me and big thanks in advance!

Link to comment
Share on other sites

Hi Tomo83 and welcome to PW.

I think you just want:

$player->playername

You are looking for the value of the field "playername" on the page that is referenced by the variable $player.

If you use:

$player->fields->get("playername") 

you'd actually most likely be looking for things like:

$player->fields->get("playername")->label

or:

$player->fields->get("playername")->description
Edited by adrian
  • Like 3
Link to comment
Share on other sites

Hi Tomo83 and welcome to PW.

I think you just want:

$player->playername

You are looking for the value of the field "playername" on the page that is referenced by the variable $player.

If you use:

$player->fields->get("playername") 

you'd actually most likely be looking for things like:

$player->fields->get("playername")->label

or:

$player->fields->get("playername")->description

Hi adrian,

ok it was that simple... :-)

$player->playername

did the job. Thank you!

  • Like 1
Link to comment
Share on other sites

  • 5 months later...
  • 9 months later...

I'm have a problem re-populating some data based on this code which doesn't seem to work.

$form_field = $templates->get('template_name')->fields;
$page = $pages->get('template=template_name,id=123');

foreach($form_fields as $field) {
  $_POST[$field->name] = $page->fields->get($field->name);
}

The page has mixed elements, so I just need to retrieve the actual values of each page field.

Instead the obove returns objects.

What am I missing?

Link to comment
Share on other sites

  • 3 years later...

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

×
×
  • Create New...