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 »

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: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: not load request with comma

Post by GanstaZ »

If your input type is int, you can't use a string. Your template input code looks like?
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: not load request with comma

Post by Kailey »

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 Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
Trubs
Registered User
Posts: 6
Joined: Thu Oct 04, 2018 7:03 pm

Re: not load request with comma

Post by Trubs »

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
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: not load request with comma

Post by Kailey »

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 Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: not load request with comma

Post by david63 »

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!

I now no longer support any of my extensions but they will start to become available here
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: not load request with comma

Post by kasimi »

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
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: not load request with comma

Post by 3Di »

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));
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3732
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: not load request with comma

Post by Kailey »

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 Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
Post Reply

Return to “Extension Writers Discussion”