Page 1 of 1
Posting Arrays
Posted: Wed Sep 21, 2011 4:43 pm
by kidcardboard
I have a form that contains two lists of check boxes for filtering. Now what I'd like to be able to do is pass all the selected values as a single array so the name for the first list would filterby[category][] and the second would be filterby[neighborhood][]. Now my problem is I can't access the values of the second level array from $params (ignore for the moment that there is no module id on those names, that isn't the issue and have been remove for legibility).
If I call the value of $params['filterby'] I get an array containing two indexes. Great that's what I'm expecting, but if I try $params['filterby']['category'] or $params['filterby']['neighborhood'] all I get returned is 'Array'. The cms seems to be to-stringing the value in the second level array.
Does anyone know a way around this or am I going to have to stick with using $_POST and sanitizing the data myself?
Re: Posting Arrays
Posted: Wed Sep 21, 2011 6:40 pm
by spcherub
You may want to do something like print_r($params['filterby']) and see exactly what is coming back. This will show you exactly how the variable is structured and you can write your code accordingly.
-S
Re: Posting Arrays
Posted: Wed Sep 21, 2011 8:18 pm
by kidcardboard
It comes back exactly as I described it
Code: Select all
Array
(
[category] => Array
[neighborhood] => Array
)
Re: Posting Arrays
Posted: Wed Sep 21, 2011 9:00 pm
by spcherub
Curious. print_r is support to be recursive. Try
Code: Select all
print_r($params['filterby'].category);
or
print_r($params['filterby']['category']);
-S
Re: Posting Arrays
Posted: Wed Sep 21, 2011 9:14 pm
by kidcardboard
print_r($params['filterby'].category); returns
Code: Select all
Notice: Use of undefined constant category - assumed 'category' in...
Arraycategory
print_r($params['filterby']['category']); returns
Re: Posting Arrays
Posted: Thu Sep 22, 2011 7:10 am
by fredp
Hmmm. What is returned when you execute this statement...
Code: Select all
echo is_array($params['filterby']['category']) ? 'Array' : 'not an Array';
Re: Posting Arrays
Posted: Thu Sep 22, 2011 4:51 pm
by kidcardboard
it returns
aswell as
Code: Select all
Notice: Array to string conversion in /var/www/.../httpdocs/lib/misc.functions.php on line 417
which I suppose I should've mentioned before because this message isn't new (I hinted to it in the op though) but it might spark something for you guys
Re: Posting Arrays
Posted: Fri Sep 23, 2011 8:46 am
by fredp
kidcardboard wrote:it returns
aswell as
Code: Select all
Notice: Array to string conversion in /var/www/vhosts/meetingscalgary.com/subdomains/dev/httpdocs/lib/misc.functions.php on line 417
which I suppose I should've mentioned before because this message isn't new (I hinted to it in the op though) but it might spark something for you guys
Hmm. That's what I suspected--not an Array.
Without knowing the version of CMSMS that you're using, it's hard to tell what code is at line 417 in the lib/misc.functions.php installed on your site.
Re: Posting Arrays
Posted: Fri Sep 23, 2011 4:51 pm
by kidcardboard
I suppose I could've included that too eh...lol. I'm running 1.9.4.2 "Faanui"
Re: Posting Arrays
Posted: Tue Sep 27, 2011 12:53 pm
by tomgsd
I've just come across the same issue so I did some checking.
On the front end, parameters are passed to the cleanParamHash method in misc.functions.php for cleaning. Unfortunately, this method does't recurse down the arrays so they are interpreted as strings, hence the "Array to string..." notice.
It doesn't look to me like there's a way round this, short of adding it as a feature request. I think the best option in this case is to just register the params separately or like you said, use $_POST and sanitize yourself.
Re: Posting Arrays
Posted: Tue Sep 27, 2011 8:23 pm
by fredp
tomgsd wrote:...
It doesn't look to me like there's a way round this, short of adding it as a feature request. I think the best option in this case is to just register the params separately or like you said, use $_POST and sanitize yourself.
@kidcardboard
I'm not sure if it would help much, but you might explore registering your "array of arrays" param as CLEAN_NONE. You'd still need to clean the array elements yourself, as with $_POST, but that would keep param handling as consistent as possible... until your feature request gets implemented. Just a thought.