assing variable in if() from condition...

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

assing variable in if() from condition...

Post by andreask »

Hello,

OK here is the stupid question of the year.

Is it possible to have something like this?

Code: Select all

if ($variable=($this->request->variable('something', '') || $this->request->variable('something_else', '')) && $another_variable['some_data'])
		{
			// do something with $variable
		}
Of course what I want with $variable is the value from the $this->$request and not the bool response of the condition.
How would you write this code more elegantly?

Thank you for your help!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
rxu
Extensions Development Team
Posts: 3712
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Contact:

Re: assing variable in if() from condition...

Post by rxu »

This code will assign the result of $this->request->variable('something', '') to $variable, then evaluate it within the if condition, and if the whole if condition is true it'll go with your code inside with the $variable value assigned.
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: assing variable in if() from condition...

Post by 3Di »

@andreask Well, that's not really an elegant way to write some code despict it could do ? work. But, how could it work since you do not want a BOOL as a result? Could you please define your vars in a programmatically way? Morevoer, what's your goal at the end of all?
🆓 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
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: assing variable in if() from condition...

Post by andreask »

@3Di I know that's why I asked...

I don't know, I guess I was thinking too "creatively"?
I was hoping that $variable would be populated by the result of the functions within the condition.

Code: Select all

($this->request->variable('something', '') || $this->request->variable('something_else', ''))
So I would get the result of $this->request->variable('something', '') OR the result of $this->request->variable('something_else', '').
Like I said don't ask why, I was probably high on tee... :D

Then I realized 2 things...
1st The variable that I am requesting is from a submit button and the value= of it is just the translation { lang('SUBMIT') } for the button. So I don't get the value of id= thus it makes it unusable.
2nd and most important of course the result of the condition (of course) is BOOL and NOT the result of functions with it. :lol: (again, stupid me. High and weird expectations).

Anyway.
I used $this->request->is_set() instead of $this->request->variable() and after that in the if(){ HERE } I used them again to execute what is needed.
Unfortunately I haven't uploaded the ext to the github yet so I can't really show it to you to see the whole picture.


But here is the if thing...

Code: Select all

if (($this->request->is_set('secretsanta_reset') || $this->request->is_set('secretsanta_reset_pair')) && $secret_santas_info['secretsanta_organizer'])
		{
			$requested = '';
			$user_ids = array_column($secretsantas['participants'], 'user_id');
			
			if ($this->request->is_set('secretsanta_reset'))
			{
				// reset participants
				$requested = 'secretsanta_reset';
			}
			
			if ($this->request->is_set('secretsanta_reset_pair')){
				// reset pairs
				$requested = 'secretsanta_reset_pair';
			}
			
			$this->reset_function($requested, $user_ids);
		}
I couldn't think anything shorter than that.
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
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: assing variable in if() from condition...

Post by 3Di »

I'm probably too tired today, but I'm having hard times here my friend.. :geek:
I'll see if I can take a look at all "this" within the next few days, in the meantime please put everything on github, I think it's better.
🆓 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
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: assing variable in if() from condition...

Post by andreask »

No problem, yes I'll upload as soon as I have a more workable ext.
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
rxu
Extensions Development Team
Posts: 3712
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Contact:

Re: assing variable in if() from condition...

Post by rxu »

andreask wrote: Sat Nov 27, 2021 3:16 pm 2nd and most important of course the result of the condition (of course) is BOOL and NOT the result of functions with it.
The result is being evaluated as bool at the end but values of expressions are having results they should return. So the result of $this->request->variable('something', '') is not bool but what is expected from this expression.
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: assing variable in if() from condition...

Post by 3Di »

andreask wrote: Sat Nov 27, 2021 3:33 pm No problem, yes I'll upload as soon as I have a more workable ext.
I just want to have a better view of your code, I do not mind if it is working or not.
Based on your code I think I am able to see your goal, at least.
🆓 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
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: assing variable in if() from condition...

Post by andreask »

Well, github was "down"... :|

But now it's up and running so here it is.
https://github.com/andreaskou/secretsanta_phpbb
And this one is the condition under question...
Of course modified to work as suppose to.
Last edited by andreask on Tue Nov 30, 2021 2:12 pm, edited 1 time in total.
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
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: assing variable in if() from condition...

Post by 3Di »

Code: Select all

if (
	($this->request->is_set('secretsanta_reset') || $this->request->is_set('secretsanta_reset_pair'))
	&& $secret_santas_info['secretsanta_organizer']
)
{
	$user_ids = array_column($secretsantas['participants'], 'user_id');

	$requested = $this->request->is_set('secretsanta_reset') ? 'secretsanta_reset' : 'secretsanta_reset_pair';

	$this->reset_function($requested, $user_ids);
}
Or

Code: Select all

if (
	($this->request->is_set('secretsanta_reset') || $this->request->is_set('secretsanta_reset_pair'))
	&& $secret_santas_info['secretsanta_organizer']
)
{
	$requested = $this->request->is_set('secretsanta_reset') ? 'secretsanta_reset' : 'secretsanta_reset_pair';

	$this->reset_function($requested, array_column($secretsantas['participants'], 'user_id'));
}
Not tested.
🆓 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
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: assing variable in if() from condition...

Post by 3Di »

Btw, if your requests are coming from someone submitting a form (button) please consider to use
(this) $this->request->is_set_post()
and add_form_key() in PHP together with {S_FORM_TOKEN} in HTML
for security reasons (preventing CSRF attacks).
🆓 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
andreask
Registered User
Posts: 752
Joined: Fri Feb 27, 2009 6:13 pm
Name: Andreas

Re: assing variable in if() from condition...

Post by andreask »

Thanks 3Di,

I've tried to use is_set_post() but for some reason I get ???? as a value...
I think add_form_key() is used. But I'll double check!
Here is what I am working on right now...
Inactive User Manager for phpBB
Give it a try...
If you would like to buy me a bier ;) for my work I will drink it on a hot summer day and thank you!!!
Post Reply

Return to “Extension Writers Discussion”