Posting Arrays

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Posting Arrays

Post 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?
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: Posting Arrays

Post 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
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Posting Arrays

Post by kidcardboard »

It comes back exactly as I described it

Code: Select all

Array
(
    [category] => Array
    [neighborhood] => Array
)
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: Posting Arrays

Post 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
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Posting Arrays

Post 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

Code: Select all

Array
fredp
Forum Members
Forum Members
Posts: 218
Joined: Sun Jul 27, 2008 1:36 am
Location: USA

Re: Posting Arrays

Post by fredp »

Hmmm. What is returned when you execute this statement...

Code: Select all

echo is_array($params['filterby']['category']) ? 'Array' : 'not an Array';
Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Posting Arrays

Post by kidcardboard »

it returns

Code: Select all

not an Array
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
Last edited by kidcardboard on Fri Sep 23, 2011 4:52 pm, edited 1 time in total.
fredp
Forum Members
Forum Members
Posts: 218
Joined: Sun Jul 27, 2008 1:36 am
Location: USA

Re: Posting Arrays

Post by fredp »

kidcardboard wrote:it returns

Code: Select all

not an Array
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.
Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln
User avatar
kidcardboard
Forum Members
Forum Members
Posts: 54
Joined: Mon Sep 28, 2009 5:25 pm

Re: Posting Arrays

Post by kidcardboard »

I suppose I could've included that too eh...lol. I'm running 1.9.4.2 "Faanui"
tomgsd
Forum Members
Forum Members
Posts: 74
Joined: Tue Feb 12, 2008 10:00 am

Re: Posting Arrays

Post 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.
fredp
Forum Members
Forum Members
Posts: 218
Joined: Sun Jul 27, 2008 1:36 am
Location: USA

Re: Posting Arrays

Post 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.
Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln
Post Reply

Return to “Developers Discussion”