• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Array Question

#1
Hi,

I'm trying to create an add-on that uses multi-selection check boxes (such as admin.php?options/edit-option/spamDefaultOptions).

I've managed to get the output I want by simply editing the sub-array names to be what I want and then using the following in my template;

AddOn_Template said:
<xen:if is="{xen:checked $xenOptions.Option ID.Array Sub-Option}">Content</xen:if>
However, I don't really understand what the values in array mean.

admin.php?options/edit-option/spamDefaultOptions said:
a:5:{s:8:"ban_user";s:1:"1";s:15:"delete_messages";s:1:"1";s:14:"action_threads";s:1:"1";s:9:"check_ips";s:1:"1";s:10:"email_user";s:1:"0";}
I have figured out that a:5 is because you have 5 array sub-options and the numbers in quotes (1 or 0) appear to dictate whether the check box should be checked (1) or unchecked (0) by default. It is the rest of the letters/numbers I don't understand, s:1, s:9, etc.

What does these values mean? Any help greatly appreciated.
 
#3
Sorry, I don't understand what you mean as my knowledge of PHP is non existant.

s denotes "serialised" and the number the order in the series which the sub-option appears/is executed? If it's possible to un-serialise an array, then what befits does serialising have to begin with?

Could the above example be simplified to something like;

AddOnOption said:
a:5:{"value_1";"1";"value_2";"1";"value_3";"1";"value_4";"1";"value_5";"0";}
?

I'm able to get working multiple check boxes to work without creating any php files (such as listener.php, addon.php etc., which I can't fully get my head round) by doing the following options;

admin.php?options/edit-option/AddOn said:
Edit Format said:
Check Boxes
Format Parameters said:
value_1 = Value 1
value_2 = Value 2
value_3 = Value 3
Data Type said:
Array Sub-Options said:
value_1
value_2
value_3
The issue arises as I'd like to be able to specify which of the check boxes are pre-checked and which aren't. Is this possible without going through the process of listener and other php files?
 

g0rn

Well-known member
#4
Well, if you execute this code
PHP:
$a = unserialize('a:5:{s:8:"ban_user";s:1:"1";s:15:"delete_messages";s:1:"1";s:14:"action_threads";s:1:"1";s:9:"check_ips";s:1:"1";s:10:"email_user";s:1:"0";}');
print_r($a);
print(serialize($a));
You will get this:
Code:
Array
(
     [ban_user] => 1
     [delete_messages] => 1
     [action_threads] => 1
     [check_ips] => 1
     [email_user] => 0
)
a:5:{s:8:"ban_user";s:1:"1";s:15:"delete_messages";s:1:"1";s:14:"action_threads";s:1:"1";s:9:"check_ips";s:1:"1";s:10:"email_user";s:1:"0";}
Serialization is a way to put a variable (array in this case) to string.
If you wish to know what the "s" means, it represents string type, and the number after it represents string length, but you shouldn't really care about it, you need only to use serialize() and unserialize() functions and manipulate with arrays as usual.
 
#5
Ah, I get it now.

The reason I couldn't get it to work for my add on was that I had the wrong string length for my array sub options.

I changed;

Default Value said:
a:5:{s:8:"ban_user";s:1:"1";s:15:"delete_messages";s:1:"1";s:14:"action_threads";s:1:"1";s:9:"check_ips";s:1:"1";s:10:"email_user";s:1:"0";}
to;

Default Value said:
a:3:{s:7:"value_1";s:1:"1";s:7:"value_2";s:1:"1";s:7:"value_3";s:1:"0";}
and then it worked as expected.

Thanks to both of you for your help :).