Jump to content

Forms, Pagination, Multidimensional Array


olafgleba
 Share

Recommended Posts

Hi,

it would be nice, if someone has a hint for me. I try to put it short (and hopefully the issue is more PW than PHP related ;-)).

I have a search form which has several (named, multidimensional) filter groups to narrow down the results. Form method is GET.

Bare, example filter group, limited to only 2 filter options:

<ul>
  <li>
    <input type="checkbox" name="type_of_audience[adults]" id="adults" value="1" 
       <?php if(isset($_GET['type_of_audience']['adults']){ echo "checked"; } ?> />
	<label for="adults" class="label-checkbox">
  		Adults
	</label>
  </li>
  
  <li>
    <input type="checkbox" name="type_of_audience[children]" id="children" value="2" 
       <?php if(isset($_GET['type_of_audience']['children']){ echo "checked"; } ?> />
	<label for="adults" class="label-checkbox">
  		Children
	</label>
  </li>
  
  ... other options ...
</ul>

After submitting the form, the results are fine and the activated checkboxes remains being checked. The query string looks like this:

http://<local-path>/?type_of_audience[adults]=1&type_of_audience[children]=2

If the result is bigger than the query limit, so that the pagination is shown, the pagination query string differs from the above. It then looks like this:

http://<local-path>/page2?type_of_audience=1,2

The pagination result pages are valid/correct, however. But as i have lost the keys of the filtergroup, the condition within the checkboxes markup (s. above), to keep the activated checkboxes being checked, of course doesn't fit anymore.

So, my question is: Is there anything i can do about the query string building for the pagination? Or do i miss something other  essential?

Thx in advance, regards
Olaf

Link to comment
Share on other sites

11 hours ago, olafgleba said:

Is there anything i can do about the query string building for the pagination?

I presume you're using MarkupPagerNav. If so see the arrayToCSV option in the docs: https://processwire.com/api/ref/markup-pager-nav/#pwapi-methods-other-options

But rather than change this option consider using the whitelist to store the sanitized search input and to set the values/states in your search form instead of using $_GET. See Ryan's Skyscrapers code for examples of how to do this:
https://github.com/ryancramerdesign/skyscrapers2/blob/master/search.php
https://github.com/ryancramerdesign/skyscrapers2/blob/master/includes/search-form.php

  • Like 1
Link to comment
Share on other sites

4 hours ago, Robin S said:

I presume you're using MarkupPagerNav. If so see the arrayToCSV option in the docs: https://processwire.com/api/ref/markup-pager-nav/#pwapi-methods-other-options

But rather than change this option consider using the whitelist to store the sanitized search input and to set the values/states in your search form instead of using $_GET. See Ryan's Skyscrapers code for examples of how to do this:
https://github.com/ryancramerdesign/skyscrapers2/blob/master/search.php
https://github.com/ryancramerdesign/skyscrapers2/blob/master/includes/search-form.php

Thank you, @Robin S , for your answer. Yes, i am using the MarkupPagerNav. And have no idea why i didn't consider the MarkupPagerNav Docs... Instead i was focusing on the sanitizer class (s. below). However, with setting the arrayToCSV option to false, the arrays are kept in the original format indeed, but still looses there keys.

http://<local-path>/?type_of_audience[]=1&type_of_audience[]=2

To keep the OP short, i didn't mentioned that i already use sanitizing and the whitelist to build the selector string for $page->find(). But didn't consider using it to set the values/states you suggested (and is shown in @ryan's examples). That's a neat idea. I think this is the way to go...

Thx again, Olaf

  • Like 1
Link to comment
Share on other sites

Could be helpful for someone,- this is how it turns out...

Note: The code examples only represent the relevant snippets related to the topic. They are not illustrating a complete search form.

regards, Olaf

/**
 * PHP section
 */

// Init vars
$selector = '';
$selected = '';

/**
 * Build selector string for the search query (`$pages->find(...)`)
 * 
 * 1. `$input->get('type_of_audience')` is a array (s. HTML section below)
 * 2. Input gets sanitized into a unique variable (because in the real form
 *    there are multiple filter groups (not included in this bare example))
 * 3. The `field` variable is just a prefix for the selector string
 * 4. Join array elements to a string, Delimiter `|`
 * 5. Whitelist the sanitized value
 */

if($input->get('type_of_audience')) {
	$value_type_of_audience = $sanitizer->intArray($input->get('type_of_audience'));
	$field = "combo_productions_attributes.type_of_audience=";
	$selector .= $field.join('|', $value_type_of_audience).", ";
	$input->whitelist('type_of_audience', $value_type_of_audience);
}

/**
 * HTML form section
 *
 * Implies surrounding `<ul>` and `</ul>` element
 */
  
/**
 * Build the HTML element (Grouped Checkboxes)
 * 
 * 1. The foreach array is populated by hand in this case
 * 2. If input exists in the delivered array, set `checked` attribute
 */
  
<?php
foreach(array( '1' => 'Adults', '2' => 'Children') as $val => $label) {
  echo "<li>";
    $selected = in_array($val, $input->whitelist->type_of_audience) ? "checked='checked'" : '';
    echo "<input type='checkbox' id='$label' name='type_of_audience[$label]' $selected value='$val' />";
    echo "<label for='$label'>$label</label>";
  echo "</li>";
}
?>

 

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