Loading...
 
Features / Usability

Features / Usability


[Solved] How to check if a string exist in 2 smarty variables

posts: 8632 Israel

Hello,

I have a tracker with a relation tracker field that look like this: "Banked ,July - Bernard | $720". I'm looking for a way to detect when the month in the field's value more than 1 month in the past.

IE:
We are in August;
if value is "Banked ,July - Bernard | $720" do nothing
if value is "Banked ,June - Bernard | $720" do something

From a wiki page I have a plugin list and then a smarty template.
In the foreach loop I grab the variable "$row.tracker_field_productsboughtPaymentId_text" that output the field _text value ("Banked ,July - Bernard | $720"). So the following comparison between a variable and a string is working just fine:

Copy to clipboard
{$paymentref = $row.tracker_field_productsboughtPaymentId_text} {if $paymentref|strstr:"July"}Say hi{/if}


However I don't want to hardcode the month of the year and want to have it based on the smarty.now date. So I created a variable for "month minus 1" and now try to compare 2 variables the same ways before.
But this is not working (I tried with single quote, no quotes, no bracket parentheses...)

Copy to clipboard
{$month = "-1 month"|date_format:"%B"} {$paymentref = $row.tracker_field_productsboughtPaymentId_text} {if $paymentref|strstr:"{$month}"}Say hi{/if}


I am missing something there or I should use something else to check if a variable contain something from another variable ?

posts: 126885 United Kingdom

Surely you would need to convert both month names into numbers (using %m format) and then do something like {if $thatMonthNumber lt $thisMonthNumber}{do stuff...

If you know the month you are looking at is always the 2nd word in the string then some regex should do it with preg_match_all ?

Does that help?

posts: 8632 Israel

Thank you dear jonny.

Ok I got your idea... I tried to create a conversion additional code but though I was overdoing something for what we may have a solution.

So for the relation field value only, grab the second word with a "preg_match_all"
preg_match_all ("\s+^\s+", $paymentref, $paymentrefmonth)

and convert each month (string) by a number.
{if $paymentrefmonth eq "January"}{$paymentrefmonth = 01}{elseif $paymentrefmonth eq "February"} ....

Then do the comparison...


Upcoming Events

1)  21 Mar 2024 18:00 GMT-0000
Tiki Roundtable Meeting
2)  18 Apr 2024 18:00 GMT-0000
Tiki Roundtable Meeting
3)  16 May 2024 18:00 GMT-0000
Tiki Roundtable Meeting
4)  20 Jun 2024 14:00 GMT-0000
Tiki Roundtable Meeting
5)  18 Jul 2024 14:00 GMT-0000
Tiki Roundtable Meeting
6)  15 Aug 2024 14:00 GMT-0000
Tiki Roundtable Meeting
7)  19 Sep 2024 14:00 GMT-0000
Tiki Roundtable Meeting
8) 
Tiki birthday
9)  17 Oct 2024 14:00 GMT-0000
Tiki Roundtable Meeting
10)  21 Nov 2024 15:00 GMT-0000
Tiki Roundtable Meeting