not load request with comma

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Trubs
Registered User
Posts: 6
Joined: Thu Oct 04, 2018 7:03 pm

not load request with comma

Post by Trubs » Sat Oct 06, 2018 12:48 pm

Hello. In main_module in class main I have something like this:

Code: Select all

$config->set('test_id', $request->variable('test_id', 0));
When I put e.g.: "2" in input text, config will update to "2".
But when I put e.g.: "2,3,4", config will update to "2". Why is that? Why is comma reduced and also everything after?

User avatar
GanstaZ
Registered User
Posts: 568
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: not load request with comma

Post by GanstaZ » Sat Oct 06, 2018 1:15 pm

If your input type is int, you can't use a string. Your template input code looks like?
"When answer lies in the question,.. question becomes redundant!"

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1944
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: not load request with comma

Post by kinerity » Sat Oct 06, 2018 1:32 pm

Trubs wrote:
Sat Oct 06, 2018 12:48 pm

Code: Select all

$config->set('test_id', $request->variable('test_id', 0));
2,3,4 is not an INT. Maybe you're looking for an array?
Kailey Truscott - Community Team

Trubs
Registered User
Posts: 6
Joined: Thu Oct 04, 2018 7:03 pm

Re: not load request with comma

Post by Trubs » Sat Oct 06, 2018 1:38 pm

kinerity wrote:
Sat Oct 06, 2018 1:32 pm
Trubs wrote:
Sat Oct 06, 2018 12:48 pm

Code: Select all

$config->set('test_id', $request->variable('test_id', 0));
2,3,4 is not an INT. Maybe you're looking for an array?
Yeah. I wasn't sure how variable works. Thanks :)

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1944
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: not load request with comma

Post by kinerity » Sat Oct 06, 2018 1:48 pm

If you are only expecting numbers in your array, you can typecast the data that's being passed to it. Have a look at this line in my knowledge base extension.
Kailey Truscott - Community Team

User avatar
david63
Jr. Extension Validator
Posts: 14938
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: not load request with comma

Post by david63 » Sat Oct 06, 2018 2:00 pm

If you need to enter an array as a config value then you will need to use the config_text table - and you will need some additional code to handle it, both saving and retrieving.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3299
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: not load request with comma

Post by kasimi » Sat Oct 06, 2018 7:16 pm

kinerity wrote:
Sat Oct 06, 2018 1:48 pm
you can typecast the data that's being passed to it
Passing an array as 2nd argument to variable() only works for field names ending with [], for example ids[]=1&ids[]=2. As far as I'm aware there's no type you can pass that makes it split a string for you.
david63 wrote:
Sat Oct 06, 2018 2:00 pm
If you need to enter an array as a config value then you will need to use the config_text table
Not necessarily. If you only ever need to store a handful of IDs, using $config is perfectly fine. Why not go the manual way:

Code: Select all

$ids = $request->variable('ids', '');           // get as string
$ids = explode(',', $ids);                      // to array
$ids = array_filter(array_map('intval', $ids)); // filter integers
$ids = implode(',', $ids);                      // back to string
$config->set('ids', $ids);                      // store string
If you need to store many IDs, consider creating a new database table for them.

User avatar
3Di
Registered User
Posts: 12998
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: not load request with comma

Post by 3Di » Sat Oct 06, 2018 7:30 pm

Using a Json array on a normal $config with a small amount of values so to not exceed 255..

Code: Select all

			/* Steam original colors set*/
			array('config.add', array('threedi_steamsuite_css_colors', json_encode(array(
							'vp_bkg' => '#273B52',
							'vp_ing' => '#90ba3c',
							'vp_onl' => '#66C0F4',
							'vp_ofl' => '#898989',
							'vt_bkg' => '#273B52',
							'vt_ing' => '#90ba3c',
							'vt_onl' => '#66C0F4',
							'vt_ofl' => '#898989',
							'icondark' => false,
						)
					)
				)
			),
Retrieve

Code: Select all

$color_configs = json_decode($config['threedi_steamsuite_css_colors'], true);
Usage of $request->variable

Code: Select all

				$color_configs['vp_bkg'] = $request->variable('vp_bkg', $color_configs['vp_bkg']);
				$color_configs['vp_ing'] = $request->variable('vp_ing', $color_configs['vp_ing']);
				$color_configs['vp_onl'] = $request->variable('vp_onl', $color_configs['vp_onl']);
				$color_configs['vp_ofl'] = $request->variable('vp_ofl', $color_configs['vp_ofl']);
				$color_configs['vt_bkg'] = $request->variable('vt_bkg', $color_configs['vt_bkg']);
				$color_configs['vt_ing'] = $request->variable('vt_ing', $color_configs['vt_ing']);
				$color_configs['vt_onl'] = $request->variable('vt_onl', $color_configs['vt_onl']);
				$color_configs['vt_ofl'] = $request->variable('vt_ofl', $color_configs['vt_ofl']);
				$color_configs['icondark'] = $request->variable('icondark', $color_configs['icondark']);
				
				$config->set('threedi_steamsuite_css_colors', json_encode($color_configs));
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate

:game_die: The new Dice Roller extension for phpBB 3.2 is out! :game_die:

Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1944
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: not load request with comma

Post by kinerity » Sat Oct 06, 2018 8:26 pm

kasimi wrote:
Sat Oct 06, 2018 7:16 pm
Passing an array as 2nd argument to variable() only works for field names ending with [], for example ids[]=1&ids[]=2.
Thank you! That's what I was trying to say and I guess it came out wrong.
Kailey Truscott - Community Team

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: Google [Bot] and 247 guests