Bit of guidance with Checkboxes custom Field Topic is solved

Everything to do with Joomla! 3.x templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
joom-rookie
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Thu Mar 03, 2022 3:29 am

Bit of guidance with Checkboxes custom Field

Post by joom-rookie » Mon Jul 17, 2023 4:24 am

Hi guys,
I have a feeling this will be a very simple oversight somewhere... but I've been stuck on this for a couple of hours and it's driving me crazy.

...I can work around this... but my curiosity wont let me leave it alone. :-[

I've created a custom field, Type: Checkboxes.
I've added a few checkboxes entries, filled out the "Text" and "Value" fields.
Set to "Do not display Automatically"
Selected them in the article and Saved it.

When I show the checkboxes in an override, I want to display the "Value" field, but the "Text" field is displayed instead.

Im using the results for <img> tags in the template. See my code below.
I've tried using $checkboxes->rawvalue which returns nothing.
Tried json_decode to get the rawvalue, but nothing.

Code: Select all

<?php foreach($this->item->jcfields as $jcfield) {
    $item->jcFields[$jcfield->name] = $jcfield;
}

$checkboxes = $item->jcFields['colours'];
	
  if (!empty($checkboxes->value)) {
    $checkboxValues = explode(', ', $checkboxes->value);

    foreach ($checkboxValues as $value) {
      echo '<img src="/images/colour-swatches/' . $value . '.png" alt="' . $value . '">';
    }
  }
?>
Thanks guys.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Bit of guidance with Checkboxes custom Field

Post by SharkyKZ » Mon Jul 17, 2023 5:34 am

Using rawvalue should work. But it's already an array so don't need the explode():

Code: Select all

if (!empty($checkboxes->rawvalue)) {
    foreach ($checkboxes->rawvalue as $value) {
        echo '<img src="/images/colour-swatches/' . $value . '.png" alt="' . $value . '">';
    }
}

joom-rookie
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Thu Mar 03, 2022 3:29 am

Re: Bit of guidance with Checkboxes custom Field

Post by joom-rookie » Mon Jul 17, 2023 8:31 am

Thanks @SharkyKZ that did indeed work... I swear I tried this very early on.
Fresh set of eyes on a problem is always good!

What if I want to use both, the Label and the Value in that same foreach loop?

Thanks again!

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Bit of guidance with Checkboxes custom Field

Post by SharkyKZ » Mon Jul 17, 2023 8:56 am

There are many ways. One is to simply use numeric keys to access values of another array:

Code: Select all

$labels = explode(', ', $checkboxes->value);

foreach ($checkboxes->rawvalue as $k => $value) {
    echo $labels[$k] . ' ' . $value;
}
Or reversed:

Code: Select all

$labels = explode(', ', $checkboxes->value);

foreach ($labels as $k => $label) {
    echo $label . ' ' . $checkboxes->rawvalue[$k];
}
Another option is to use array_combine() function to create a single array with values as keys and labels as values:

Code: Select all

$values = array_combine($checkboxes->rawvalue, explode(', ', $checkboxes->value));

foreach ($values as $value => $label) {
    echo $label . ' ' . $value;
}

joom-rookie
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Thu Mar 03, 2022 3:29 am

Re: Bit of guidance with Checkboxes custom Field

Post by joom-rookie » Wed Jul 19, 2023 2:09 am

I really like that first example, so clean! Thanks @SharkyKX, you've been really helpful!


Post Reply

Return to “Templates for Joomla! 3.x”