Jump to content

It looks so easy, checkbox


bwakad
 Share

Recommended Posts

I thought, after reading a article from ryan about asm select, maybe it IS easier to make use of a checkbox.

Simply check the box - then display what you want. That way, I only need 6 checkboxes, against 40 pages.

So instead of making a lot of pages for a certain multi select, I began to make my first checkbox field. As an example:

Name: skill_one

Label: skill one

Description text: skiil one holds item1, item2, item,3

And thinking I could then display the name and description, I first started with echo $page->skill_one

I ended up with either 0 or 1, which probably makes sense. But ->name or ->title or ->description does nothing.

So how do I display the name, label or description. I can not find that anywhere?

Sometimes, I wish there was also a cheatsheet for more fieldtypes with some examples. As a beginner, how was I to know a multi select is to be displayed with foreach or implode? The API simply stated $page->fieldname->title. Meanwhile I explode. lol

Link to comment
Share on other sites

One is certain you won the time/question award. :P

You need to fix the logic here I think anyway but. The field here just returns a value as you see. There's no name or title. You would grab the field VIA template or fieldgroup. There it's an field object you can get label or description. I would go for a page field with input checkboxes.

Link to comment
Share on other sites

Oh and cheatsheet v2 extended reloaded is coming sooner or later to your nearby shop. Just append a variable name manually at the end of url. Like cheatsheet.processwire.com/page/.

Link to comment
Share on other sites

hahaha!

especially for me? wow! lol

But listen, I did use the $page->fieldname->title on my template. So what exactly do you mean by 'there it is a object' since I tried that...

I did not even know I could have a page then input field type checkbox - so thank you.

But also this field get displayed with id's. 

As soon as I use in a foreach $jobs as $job ... $job->fieldname->title it's gone...

I must be missing something :-X

EDIT ----

My bad, since it is now an array, I had to use implode!!! :biggrin:

Link to comment
Share on other sites

You can definitely foreach over an array - no need to use implode. Implode is usually reserved for when you want to convert an array into a string with a particular separator between each item in the array - you could still use foreach for this as well - it's a matter of choosing the cleanest, most efficient and readable option in the given situation. If you are having trouble with the foreach, show us your code.

Link to comment
Share on other sites

Yes! A page field with multiple options, it's an PageArray.

$pageArray->implode() is one utility to deal with it. Or just use on of the array methods you also see on cheatsheet.

Others useful things are

$page->pagefieldmulti->first()->title;

Translates to:

"currentPage->thepagefieldpagearray->thefirstinthearray->itstitle"

Or in a foreach

foreach($page->pagefieldmulti as $selected) echo $selected->title;

Or to search and check for if a certain option is selected

if($page->pagefieldmulti->has(1001)) echo "option page with ID 1001 is selected";

has($page) return true if the PageArray of the field contains the page you give it.

$page->pagefieldmulti->has($someotherpage);

Since the options of a page field are also pages, it may get clearer if you try to think of it as a array of pages. Many methods accepts various things like a string or object or int of an ID maybe even a path like "/some/path/to/a/page". 

Just play and experiment with it and see what works.

  • Like 1
Link to comment
Share on other sites

The page field I created, is of type checkboxes, and holds five pages. The field is called 'check'. Page names are one, two, three, four, five.

It is a single page for now, only one checkbox can be chosen. Multi page would be great! But I need to display the chosen value and a description, for example : value 'one', description 'appel, peer, sinas'.

edit - I will try with your code examples to find out by myself. Thanks for putting me on track. DO I GET MY TROFEE NOW :)???

$jobs = $pages->find('template=job, parent.template=jobs');

if (count($jobs)) { // only if we have jobs   
    //$out = "<div>";

    foreach ($jobs as $job) {
        $out  = "<div class='item'>"
              . "<div><h3><a href='$job->url'>" . $job->title."</a></h3></div>"
              . "<div class='bold'>"
              . "<div>" . $job->contract->title . "</div>"
              . "<div>" . $job->branche->title . "</div>"
              . "<div>" . $job->education->title . "</div>"
              . "<div>" . // here I need to display the value chosen and optinal text for this value //  ."</div>"
              . "</div>"
              . "<div" . trunc($job->body, 190) . "</div>"
              . "</div><hr>"
              ;

    echo $out;     
        }  
    }
Link to comment
Share on other sites

Oh and cheatsheet v2 extended reloaded is coming sooner or later to your nearby shop. Just append a variable name manually at the end of url. Like cheatsheet.processwire.com/page/.

Hey! thats very cool...

Link to comment
Share on other sites

foreach($page->pagefieldmulti as $selected) echo $selected->title; // worked excellent

if($page->pagefieldmulti->has(1001)) echo "option page with ID 1001 is selected"; // worked not

So I took the first code.

foreach($job->check as $selected) {
            echo $selected->title;
            echo $selected->body;
        }

So in pagefieldmulti I chose the parent page (for selectable childs) that will function as checkboxes.

I then made a separate template for these child pages which have a title and body field.

Now when I output these 'selected' pages, it will display those two fields as a title and description.

Never once seen a cms that display multiple same fields (body in my case) in one article.

Amazing what you can do with processwire - so many possibilities!

And lucky there are many friendly people on this forum to help eachother.

More will turn to PW for sure. Because these type of things would be weeks or maybe months on other cms.

  • Like 1
Link to comment
Share on other sites

if($page->pagefieldmulti->has(1001)) echo "option page with ID 1001 is selected"; // worked not

Ah, sorry it's not $page->pagefieldmulti->has(1001) then it would have to be a selector

if($page->pagefieldmulti->has("id=1001")) ...

if($page->pagefieldmulti->has("id=1001|1002)) ...
  • 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...