Jump to content

Displaying Multi-Checkbox and Multi-Select Values: code questions


Recommended Posts

Hello All,

I've been developing some generic routines in include files, to display and edit various types of fields. This question relates to my code to display the fields on a page (i.e. "view mode", not "edit mode"). I loop through an array of field names, grab the values, and then replace tags in templates. I'm doing it this way because I'm building a front-end user app with many tables (field sets), and want to be able to reuse the code.

I've decided to use four types of Page fields for front-end display: Single Selects, Radio fields, Mutliple Checkboxes and Multiple Selects.

The issue I ran into was with Multiple Checkboxes and Multiple Selects when the field stored only ONE value. The code didn't work as I expected. I created a work around, but I'm not sure why the first method didn't work, and I'm not sure if I'm doing it the right way.

Here's the code that runs against each of the field names, for a page. I've marked the question lines in red.

if ( $field->type == 'FieldtypePage' )
      {
      # process four of the input types:
      # single select, radio, checkboxes, multiple select

      # InputfieldCheckboxes
      # InputfieldSelectMultiple


      # IS THIS THE BEST WAY TO TEST FOR MULTIPLE VALUES?


      if ( count($page->$field_name) > 1 )
            {
            $field_value = '';

            foreach( $page->$field_name as $field_value_element )
                  {
                  $field_value .= $field_value_element->select_value . '<br>';
                  }
            }
      else
            {

            # THE LINE BELOW DIDN'T WORK WHEN THE CHECKBOXES AND MULTI-SELECTS ONLY HAD ONE VALUE

            # (It worked fine for the single select and radio fields)

 

            # $field_value = $page->$field_name->select_value;

 

            # ... so I get the ID of the page, and then do a get on the "select_value" field.

 

            $field_id    = $page->$field_name;
            $field_value = $pages->get("$field_id")->select_value;

            }
      }
elseif ( $field->type == 'FieldtypeCheckbox' )
      {
      $field_value = ( $page->$field_name == 1 ) ? "Yes" : "No";
      }
elseif ( $field->type == 'FieldtypeFloat' )
      {
      $field_value = money_format('%i', $page->$field_name);
      }
elseif ( $field->type == 'FieldtypeTextarea' )
      {
      $field_value = str_replace("\n", "<br>", $page->$field_name);
      }
else
      {
      $field_value = $page->$field_name;
      }

 

Is my work around above, with the ID and the get, the only way? I would have expected the first method to work.

Also, what do you think about the other elseif statements below the Page Field section?

Does this block of code seem reasonable as a method, to parse through various types of fields, and then get and format the values correctly?

Thanks!

Peter

Link to comment
Share on other sites

I'm not sure if we're talking about Page fields that are set to store just a single value (via their field settings) or Page fields that simply contain one value (and are able to contain more). But you mentioned the problem was with multi-value selects like select[multiple] and checkboxes, so I'll assume we're talking about multi-page storage. Meaning, the "dereference in API" setting is PageArray, not not Page. If this is the case, it should not matter whether there is no value, one value, or multiple values, as they should all be treated the same. So if you are seeing different behavior in 1 vs multiple, then the first place I would look is in your field settings to see what they are set to be on the API side (single vs. multiple). 

Link to comment
Share on other sites

Dear Ryan,

The two fields that are having problems are set to type "Page" and "Multiple pages (PageArray)", and then "Checkboxes" and "SelectMultiple", respectively.

When they have more than one value, they display them correctly. When there's only one value, the code didn't work, so I had to modify it, thus:

            # $field_value = $page->$field_name->select_value;


            $field_id    = $page->$field_name;
            $field_value = $pages->get("$field_id")->select_value;

I just checked the settings, as you mentioned, and they seem fine.

Best regards,

Peter

Link to comment
Share on other sites

As Ryan said, Page fields either can be multiple or single.

multiple = PageArray (array)

single = Page (value)

It doesn't matter how many values are stored. PageArray will always be PageArray even if it has only 1 entry. 

multiple = [page1,page2,page3]

multiple = [page3]

single = page4

So checking for count(PageArray) > 1 will not produce expected results.

Possible solutions I gave you in your other thread here http://processwire.com/talk/topic/3915-api-syntax-to-edit-field-value-in-form-with-pw-settings-and-attributes/?p=38943

Link to comment
Share on other sites

Dear Ryan and Soma,

Thank you both for your help. Soma, I *thought* I had looked at your "derefAsPage" suggestion, but I must have done it wrong, because now it works, with both single and multiple and empty values.

      if( $field->derefAsPage == 0)
            {
            # is array
            $field_value = '';
            $total_pages = count( $page->$field_name );
            $count       = 0;

            foreach( $page->$field_name as $field_value_element )
                  {
                  $count++;

                  if ( $count < $total_pages )
                        {
                        $field_value .= $field_value_element->select_value . '<br>';
                         }
                  else
                        {
                        $field_value .= $field_value_element->select_value;
                        }
                  }
            }
      else
            {
            $field_value = $page->$field_name->select_value;
            }

 

I must have missed something, but now it all seems good.

Thank you both!!!

Peter

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