Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Open to all! Ask other shopowners for help.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by loop »

Hi All
I'm building a attribute Product Filter in the index.php and it works, but i'm getting at the bottom of the page the error because of the rawurlencode()

Code: Select all

Warning: rawurlencode() expects parameter 1 to be string, array given in /mnt/lamp-www-data/beta_shop/includes/system/versioned/1.0.8.5/href.php on line 106
I know the Reason, it's because of the Filter the checkboxes for manufacturer have for example name "manufacturers_filter_id_arr" and if i chose multiple manufacturer to filter the form which is submitet is:

index.php?cPath=191_663_656_963&current_category_id=963&manufacturers_filter_id_arr[]=1494&manufacturers_filter_id_arr[]=1492

My filter works as it should, but i don't know how i can get the listing->split to work with the url which has array in it...

in split_page_results.php is the link created "$link->set_parameter($this->page_name, $jump_to_page)"

but maybe there is a smarter solution as change the link creater on the split_page_results.php

any ideas?

thank you all!
Philipp


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by ecartz »

loop wrote: Tue Mar 01, 2022 9:45 am I'm building a attribute Product Filter in the index.php and it works, but i'm getting at the bottom of the page the error because of the rawurlencode()
So the first page works in that it shows you the expected results, but the page links are broken because retain_query_except does not handle the array correctly? You could use the second parameter to display_links to pass an already built link to bypass the use of retain_query_except on line 79 of split_page_results.php

Or you could try changing line 106 of includes/system/versioned/1.0.8.5/href.php to

Code: Select all

        return is_array($v) ? implode('&', array_map(function($e) use ($k) {
          return "$k" . '[]=' . rawurlencode($e);
        }, $v)) : "$k=" . rawurlencode($v);
But that would be a core change or require you to override the Href class. The display_links is on lines 31 and 99 of the product_listing component template. If you are already overriding that component, it might be easier to just pass the link that it expects.

If the core change fixes your problem, I might consider changing it in 1.0.8.14. I'm having some trouble visualizing the consequences though.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by loop »

Hi eCartz, yes that's correct.

i tried the corechange to give you feedback, but it gives a error:

Code: Select all

$parameters = implode('&', array_map(function ($k, $v) {
        return is_array($v) ? implode('&', array_map(function($e) using ($k) {
          return "$k" . '[]=' . rawurlencode($e);
        }, $v)) : "$k=" . rawurlencode($v);
      }, array_keys($this->parameters), $this->parameters));
Parse error: syntax error, unexpected 'using' (T_STRING), expecting '{' in /mnt/lamp-www-data/beta_shop/includes/system/versioned/1.0.8.5/href.php on line 106
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by ecartz »

Sorry, it's use, not using. I edited the code in the previous post.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by loop »

the error because of the "using" is gone, but the error rawurlencode() is still here.

Warning: rawurlencode() expects parameter 1 to be string, array given in /mnt/lamp-www-data/beta_shop/includes/system/versioned/1.0.8.5/href.php on line 107

i tried to debug it, it's because in the $e is still array:
print_R($e) = Array ( [0] => 8K Ultra HD )

it's because i have also in 1 filter multidimensional arrays :(

snippet from the url "&techspec_fields[B22_ICEHD-TYP][]=8K+Ultra+HD"
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by ecartz »

Revert back to the original and add to Href the following method:

Code: Select all

      public function build_subquery(array $parameters, string $prefix) {
        return implode('&', array_map(function ($k, $v) use ($prefix) {
          $k = "{$prefix}[{$k}]";
          return is_array($v) ? $this->build_subquery($v, $k) : "$k=" . rawurlencode($v);
        }, array_keys($parameters), $parameters));
      }
and change line 106 to

Code: Select all

return is_array($v) ? $this->build_subquery($v, $k) : "$k=" . rawurlencode($v);
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by loop »

it worked! thank you! (but now i changed core ....maybe you consider to add it in the next releases...? ;)
thank you very much for your great help!
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by loop »

Hi ecartz
I think i have hte same problem now here, as i have parameter, filters, as a array (filter[]=test) and the sort function in de index.php (sort for name or price) cannot handle this

i think it's this function:

Code: Select all

public function retain_query_except(array $excludes = []) {
      $excludes = array_merge($excludes, ['x', 'y', 'error', session_name()]);
      $this->parameters += array_diff_key(array_filter($_GET, function ($k) {
        return rawurlencode($k) === $k;
      }, ARRAY_FILTER_USE_KEY), array_flip($excludes));
      return $this;
    }
you already told me how to fix the rawurlencode to accept the arrays, do you have a idea how to do it for the sort function, that it accepts them?

(for rawurl we did this in the href.php

Code: Select all

   public function build_subquery(array $parameters, string $prefix) {
      return implode('&', array_map(function ($k, $v) use ($prefix) {
        $k = "{$prefix}[{$k}]";
        return is_array($v) ? $this->build_subquery($v, $k) : "$k=" . rawurlencode($v);
      }, array_keys($parameters), $parameters));
    }
because at the moment I lose every filter as soon as i make a new sort.... :(

one simple idea would maybe be, change the function
public static function create_sort_heading($sortby, $colnum, $heading, $class = 'dropdown-item')

to this, and it will work:

Code: Select all

public static function create_sort_heading($sortby, $colnum, $heading, $class = 'dropdown-item') {

        if (!$sortby) {
          return $heading;
        }
  
        $link = $GLOBALS['Linker']->build()->retain_query_except(['info', 'page']);
        $link->set_parameter('sort', $colnum . ($sortby == $colnum . 'a' ? 'd' : 'a'));
  
        $selected = substr($sortby, 0, -1) == $colnum;
        $ascending = substr($sortby, -1) === 'a';
        $title = sprintf(($selected && $ascending) ? TEXT_DESCENDINGLY : TEXT_ASCENDINGLY, $heading);
        $text = sprintf(($selected ? ($ascending ? LISTING_SORT_DOWN : LISTING_SORT_UP) : LISTING_SORT_UNSELECTED), $heading);
  
        return '<a href="' . $_SERVER['PHP_SELF']."?".str_replace("sort=".$sortby, "sort=".$colnum."a", $_SERVER['QUERY_STRING']). '" title="' . Text::output($title) . '" class="' . $class . '">' . $text . '</a>';
      }
but maybe there is a more elegant way, it's probably a ugly hack this function
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by ecartz »

loop wrote: Thu Jul 14, 2022 5:47 pm you already told me how to fix the rawurlencode to accept the arrays, do you have a idea how to do it for the sort function, that it accepts them?
When I make that modification to Href and try to visit specials.php?manufacturers_filter_id_arr[]=1494&manufacturers_filter_id_arr[]=1492 then my sort URLs look like specials.php?manufacturers_filter_id_arr[0]=1494&manufacturers_filter_id_arr[1]=1492&sort=2a

I don't know why yours aren't working.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: Need Submitting Array in Form in the index.php - getting error because of rawurlencode()

Post by loop »

Hi eCartz
What you mean with "whenn i make the modification"? did you change something in this? because i still use 1.0.8.7 at the moment. Is something changed in the newer version about this? and if yes, in which version is it changed? (maybe i can use this function in my 1.0.8.7 until i upgrade to the newest one)

thank you!


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply