If/ElseIf/Else & custom database fields.

G. Brady

Member
Registered
Joined
Mar 15, 2022
Messages
9
Points
3

Reputation:

Hello there!

I have been stuck on this issue for some time now.

On the pages application I have a database which displays open vacancies within my community, which looks like the following:
7fuw9


When the database record is 'Closed' (custom field key status2 === 1) I want the apply button to be disabled.

When the database record is 'Hiring' (custom field key status2 === 2) I want the apply button to be enabled.

Within Database templates i'm aware that if & else statements are usable, however I can't work out how to write the if expression. I've tried everything i can think of and can not get it to work. Does anyone know how I would get this to work?

Below is an example of what I have tried.
1647304343197.png


After hours of research myself, these are the only two articles that i could find.
https://invisioncommunity.com/4guides/themes-and-customizations/template-syntax/ifelseifelse-logic-r138/
https://invisioncommunity.com/forums/topic/408319-if-statement-for-custom-field/

If anyone could work out how to do this, I will love you forever! <3
 

BattleKing

Spirit of darkness
Staff member
Administrator
Moderator
+Lifetime VIP+
S.V.I.P Member
Collaborate
Registered
Joined
May 24, 2020
Messages
3,513
Points
523

Reputation:

please try the comparison with two = instead of three, maybe this solve your issue:

==EqualReturns true if $x is equal to $y
===IdenticalReturns true if $x is equal to $y, and they are of the same type
 

G. Brady

Member
Registered
Joined
Mar 15, 2022
Messages
9
Points
3

Reputation:

please try the comparison with two = instead of three, maybe this solve your issue:

==EqualReturns true if $x is equal to $y
===IdenticalReturns true if $x is equal to $y, and they are of the same type
BattleKingI have put:

HTML:
{{if intval( $status2 ) == 1}}
<a style="margin: 5px;" class="ipsButton ipsButton_medium ipsButton_primary ipsButton_disabled" href="{$row->customFieldDisplayByKey('url', 'listing')|raw}" target="_self">Apply</a>
{{else}}
<a style="margin: 5px;" class="ipsButton ipsButton_medium ipsButton_primary" href="{$row->customFieldDisplayByKey('url', 'listing')|raw}" target="_self">Apply</a>   
{{endif}}

however both records have the second html snippet.
 
View previous replies…

BattleKing

Spirit of darkness
Staff member
Administrator
Moderator
+Lifetime VIP+
S.V.I.P Member
Collaborate
Registered
Joined
May 24, 2020
Messages
3,513
Points
523

Reputation:

I have put:

HTML:
{{if intval( $status2 ) == 1}}
<a style="margin: 5px;" class="ipsButton ipsButton_medium ipsButton_primary ipsButton_disabled" href="{$row->customFieldDisplayByKey('url', 'listing')|raw}" target="_self">Apply</a>
{{else}}
<a style="margin: 5px;" class="ipsButton ipsButton_medium ipsButton_primary" href="{$row->customFieldDisplayByKey('url', 'listing')|raw}" target="_self">Apply</a>   
{{endif}}

however both records have the second html snippet.
G. Bradytry to display the value of intval($status2) to see what is in there.
 

BattleKing

Spirit of darkness
Staff member
Administrator
Moderator
+Lifetime VIP+
S.V.I.P Member
Collaborate
Registered
Joined
May 24, 2020
Messages
3,513
Points
523

Reputation:

BattleKing

Spirit of darkness
Staff member
Administrator
Moderator
+Lifetime VIP+
S.V.I.P Member
Collaborate
Registered
Joined
May 24, 2020
Messages
3,513
Points
523

Reputation:

Here's what {$row->customFieldDisplayByKey('status2')} displays:

29j8b


{$row->customFieldDisplayByKey('status2', 'listing')|raw}:
36na0


{$row->customFieldDisplayByKey('status2')|raw}:
v82d8
G. Bradytry {{if intval( $status2 ) == 'Closed'}}

and please could you show us how you have assigned the content to $status2
 

BattleKing

Spirit of darkness
Staff member
Administrator
Moderator
+Lifetime VIP+
S.V.I.P Member
Collaborate
Registered
Joined
May 24, 2020
Messages
3,513
Points
523

Reputation:

gbse3

It does not accept the if statement you provided,

Here is the custom field settings:
lqr8o
G. Brady{{if {$row->customFieldDisplayByKey('status2', 'listing')|raw} == 'Closed' }}
or with
{{if {$row->customFieldDisplayByKey('status2', 'listing')|raw} == "Closed" }}
 

G. Brady

Member
Registered
Joined
Mar 15, 2022
Messages
9
Points
3

Reputation:

{{if {$row->customFieldDisplayByKey('status2', 'listing')|raw} == 'Closed' }}
or with
{{if {$row->customFieldDisplayByKey('status2', 'listing')|raw} == "Closed" }}
BattleKingBoth of these make this error:
The syntax of tags or HTML logic is badly formatted.
 

BattleKing

Spirit of darkness
Staff member
Administrator
Moderator
+Lifetime VIP+
S.V.I.P Member
Collaborate
Registered
Joined
May 24, 2020
Messages
3,513
Points
523

Reputation:

Both of these make this error:
The syntax of tags or HTML logic is badly formatted.
G. BradySorry, I have no running IPS board where I can check it
What happened if you display
{intval($status2}
{($status2}
 

FNC1

Member
Registered
Joined
Feb 2, 2022
Messages
9
Points
13

Reputation:

please try the comparison with two = instead of three, maybe this solve your issue:

==EqualReturns true if $x is equal to $y
===IdenticalReturns true if $x is equal to $y, and they are of the same type
BattleKingHe already put intval to his variable so both of them integer. "===" and "==" same on this expression.
 

G. Brady

Member
Registered
Joined
Mar 15, 2022
Messages
9
Points
3

Reputation:

So after some more messing around, I've found out I can use the 'display' setting to simply get a 'Closed' or 'Hiring' value. (instead of the 'listing' value which also includes all of the HTML snippet.

With this code:
HTML:
{$row->customFieldDisplayByKey('status2', 'display')|raw}

I get:
d0enx

and if the custom field is set to hiring:
j3oay


So how do i get this {$row->customFieldDisplayByKey('status2', 'display')|raw} into an if statement?
 

G. Brady

Member
Registered
Joined
Mar 15, 2022
Messages
9
Points
3

Reputation:

I have achieved the impossible!

I managed to get it to work. To do this I used the code:
{{if $row->customFieldDisplayByKey('status2', 'display') == "Closed" }}

and in the custom field enabled: 'Show in display template' then set it to no formatting.

Thank you so much @BattleKing
 

FNC1

Member
Registered
Joined
Feb 2, 2022
Messages
9
Points
13

Reputation:

I don't know how are you calling the query but just use \IPS\Db::i()->select('selected','tablename',array('id=?', $id))->first(); to bring data.
'SELECT selected FROM `tablename` WHERE id IS $id'
Also, make sure the selected item exists. Otherwise it'll return 500.

$myVar = \IPS\Db::i()->select('selected','tablename',array('id=?', $id))
if (count($myVar) != 0) $myVar = $myVar->first(); else (do something because user waits for something.)
 
Last edited:
Top