Jump to content
jploch

get checked titles of multiple checkbox option field

Recommended Posts

Hi! 
this should be easy, but I can't get it to work.

I have a repeater with events.
Every event has an option-field with multiple checkboxes.
To filter the events on the frontend I have a script, that uses the class names to filter the results (an event can have multiple categories).

Now I just want to add every checked option (title) to use as my class name.
This is what I have so far, wich only gets the first title.

foreach($page->events as $event) {

$tags = $event->options->title;
  echo "<div class='size1of2 {$tags}'>";
  echo "<img src='{$event->image->url}'>";
  echo "{$event->text_editor}";
  echo "</div>";
}


I know there is an example with a foreach, but how would I use it in this context?

foreach($page->countries as $country) {
  echo "<li>$country->title</li>";
}


 

Share this post


Link to post
Share on other sites

As an alternative to using classes to filter, you can convert categories into JSON and inject into HTML as data attributes. I haven't tried this, but it should work.

$tags = $event->options->extract('title');
$json = htmlspecialchars(json_encode($tags));
echo "<div data-category='$json'> ... </div>";

https://stackoverflow.com/a/9430472

  • Like 1

Share this post


Link to post
Share on other sites

Thx for the quick reply!
But this doesn't seems give me the correct array, it just returns nothing:

$tags = $event->options->extract('title');

 

Share this post


Link to post
Share on other sites

I assumed options field to return a type of WireArray, I've checked, it returns a SelectableOptionArray which extends WireArray, but I guess some methods are modified

I tested this one and it works

$tags = $page->options->getValues();
$tags = array_column($tags, 'title');
$json = htmlspecialchars(json_encode($tags));

echo "<div data-category='$json'> ... </div>";

In your JS, you can retrieve the array back like this

let items = document.querySelectorAll('.item');
// filter items by category
let xItems = items.filter(it => {
	let tags = JSON.parse(item.dataset.category); // tags is an array  
  	return tags.indexOf('category-x') > -1;
})

 

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, jploch said:

Now I just want to add every checked option (title) to use as my class name.
This is what I have so far, wich only gets the first title.

foreach($page->events as $event) {
	// empty tags in each iteration
	$tags = '';
	// $event->options has multiple items (multi checkboxes)...
	// ...so, we loop through it
	foreach ($event->options as $o) {
		$tags .= $o->title . ' ';// join 'em up
	}
	// remove empty spaces
	$tags = trim($tags);
	echo "<div class='size1of2 {$tags}'>";  
	echo "<img src='{$event->image->url}'>";
  	echo "{$event->text_editor}";
	echo "</div>";
}

Could be made cleaner, shorter, etc...

 

1 hour ago, abdus said:

$tags = $event->options->extract('title');

Is this a new WireArray method? I can't seem to find it

  • Like 3

Share this post


Link to post
Share on other sites
2 minutes ago, kongondo said:

Is this a new WireArray method? I can't seem to find it

I meant explode() method, but wrote extract() instead
https://processwire.com/api/ref/wire-array/explode/

And thankfully, that works @jploch.

$tags = $page->options->explode('title');
$json = htmlspecialchars(json_encode($tags));

echo "<div data-category='$json'> ... </div>";

Thank you for the heads up @kongondo

  • Like 2

Share this post


Link to post
Share on other sites

Nice!
thanks abdus and kongondo!
its all working now!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By killedfriendz
      I am very sorry for asking this but i totally do not understand how to set values of checbox using API. 
      I have checbox field on my page with name "order_status". 
      So i've tried few ways to make it checked but it still doesn't work:
       
      $userPage->order_status->value = 1; $userPage->order_status->add(1); $userPage->order_status->add(true); Could you please tell me how to do it?
    • By cosmicsafari
      Hi all,
      Before I go potentially wasting time trying to achieve the impossible.
      Can anyone confirm if its possible to have a Page Reference field on a modules config page?
      I'm wanting to essentially just output a list of select able pages based on the a given selector (likely by template at this stage), wherein the select is the pages that the module should apply to etc. I was thinking a simple checkbox list would suffice is asmSelect isn't available.
      Essentially have it display the same way a Page Reference field would display on a template, where you can easily select a bunch of them.
      public function getInputfields() { $inputfields = parent::getInputfields(); $f = $this->modules->get('InputfieldPage'); $f->attr('name', 'testSelect'); $f->setAttribute('multiple', 'checkboxes'); $f->setAttribute('findPagesSelector', 'template=development'); $f->label = 'Test'; $inputfields->add($f); return $inputfields; } Figured something akin to the above would work but can't seem to get rid of this warning on the modules config screen though.

    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
    • By RicknRoller
      In the Doc it says:
      I have an options field (options_colors) configured like this:
      I want to have a query returning only pages with the VALUE 'red'. This is a query that works:
      $newsentries = $pages->find('template=news-entry,options_colors=number1,sort=-created,limit=16'); this is a query which does not work
      $newsentries = $pages->find('template=news-entry,options_colors=red,sort=-created,limit=16'); How do I query for 'red' instead of the title for the value?
×
×
  • Create New...