Conditional Operators
Sunny
Posts: 9296
Reputation: 456

Message # 1 | 9:47 AM
Conditional operators for newbies and professionals.

If you do not understand how conditional operators work and the information in the Control Panel is not enough for you, I will try to explain. First, you should believe, that it is very easy!

Conditional operators are special codes-symbols, which can perform certain scenarios (scripts). You can create an absolutely new option on your site that is not provided in the Control Panel.

First, you must understand and get used to the components of the script code. They are:

<?if($code$)?> – it is some condition (code), which points to the operator whom and what we are dealing with. We can mentally replace it by the phrase «If there is»;

<?else?> – it is the operator which will do what you say if the condition corresponds to the indicated ones. Replace it mentally by the phrase «If not»;

<?endif?> – it is the end of the conditional operator, replace it mentally by «The end»;


Now let’s examine an easy example from the Control Panel:

(from Page Editor)

<?if($USER_LOGGED_IN$)?> Hello, $USERNAME$ <?else?> You are a GUEST. Please, register. <?endif?>

In our language it means: «If there is an authorized user, then «Hello, his/her login!», if not, «Hello, guest!» and the end.»

Now the details.

What did we use?

We said to <?if($code$)?> operator that our script is intended for saying hello to authorized users. We put the following code instead of $code$ - $USER_LOGGED_IN$ , i.e. we said «If there is an authorized user - <?if($USER_LOGGED_IN$)?>». Then the information, available only to authorized users, follows. At the same time, we may use conditions if they do not correspond to the indicated ones, in our example it is Guests. Therefore, after the information for registered users we enter the information for guests.

We get
<?if($USER_LOGGED_IN$)?> Hello, $USERNAME$ <?else?> Hello, Guest!

Now we have just to close it.
<?if($USER_LOGGED_IN$)?> Hello, $USERNAME$ <?else?> Hello, Guest! <?endif?>

We can also exclude Guests and display information for registered users only. For this purpose we just do not use «If not». We get the following:

<?if($USER_LOGGED_IN$)?> We are glad that you have registered! <?endif?>

It means that only those who have logged in will see the phrase «We are glad that you have registered!».

Any code, specified as working for a certain module, can be a condition for the work of operators. In our case (Page Editor), the following examples can be used:

Login and logout:
<?if($USER_LOGGED_IN$)?> <a href="$LOGOUT_LINK$">LOG OUT</a> <?else?> <a href="$LOGIN_LINK$">LOG IN</a> <?endif?>

Days after registration:
<?if($USER_LOGGED_IN$)?> You are with us for $USER_REG_DAYS$ days! <?else?> Please, register. <?endif?>

And so on.


Conditions for a script can be changed by means of additional capabilities, supported by the operators. They are «=», «>», «<» signs. If necessary, the equated conditions are separated by a vertical line. After the sign, you say to the operator to whom the whole script is directed.

<?if($code$='condition')?> I can see this <?else?> And here I can see this <?endif?>

Or

<?if($code$='condition')?> The main thing is that I can see this <?endif?>

Example:

<?if($USER$='John')?> John, don’t forget to read e-mail every day. <?endif?>

It means «If there are usernames and there is John among them (='John'), then this is to be displayed to him…»

The same is with groups, names, gender, date etc.
More examples:

<?if($USER$='Mike' or $USER$='Lola')?> Mike and Lola, here is a secret link …. <?endif?>

Groups:
<?if($GROUP_ID$=3 or $GROUP_ID$=4)?> Moderators and Administrators, click on the ad here… <?endif?>

And so on.

However, operators can work in each other. Example:

<?if($USER_LOGGED_IN$)?> Hello $USER$! <?if($GROUP_ID$=3 || $GROUP_ID$=4)?> Don’t forget to click on the ads! <?endif?> <?else?> Please, register. <?endif?>

The script is the following: «If there are logged-in users, then Hello, if there Administrators and Moderators among them, then add Click on ads, if there are unregistered users, we ask them to register, and the end.»


The main thing is that you must think well how to write a script, what codes to use and whom they will be directed to. You may write anything, uCoz is very flexible!

Additional mini-info:


  • The number of <?if($code$)?> and <?endif?> must be the same.
  • <?else?> is necessary if we output information for those, who don’t meet the conditions, otherwise it is not needed.
  • Alternative conditions or and and (|| and &&) are supported.
  • Nesting of operators is supported.
  • Comparing of variables to each other is supported: <?if($VAR1$=$VAR2$)?>
  • The text must be in quotes when comparing: <?if($USER$='Mike') ?>

    I'm not active on the forum anymore. Please contact other forum staff.
  • Ed05
    Posts: 556
    Reputation: 7

    Message # 181 | 4:20 PM
    Sunny, cause i dont have free modules any more for that categories that i need. Ok,i will find a way somehow..
    !
    Post edited by Ed05 - Wednesday, 2014-04-02, 4:23 PM
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 182 | 11:23 AM
    Ed05, you could also try tags.
    I'm not active on the forum anymore. Please contact other forum staff.
    MMM5264
    Posts: 11
    Reputation: 0

    Message # 183 | 9:42 AM
    hi guys, I have a little trouble
    ex: I have 2 cataloge A and B.
    A have 10 posts (A1, A2, A3 .....A10) and B have 15 posts(B1, B2, B3..... B15) .
    I want to create inform. when access B1, or B2 the inform will show related post in only cataloge B, When when access A1, or A2 the inform will show related post in only cataloge A.
    I try any way but all fail.
    Can anyone help me?

    thanks.
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 184 | 1:33 PM
    MMM5264, by standard means it is not possible. May be possible with custom scripts, but if you have a lot of entries, there is a risk of IP blocking due to too many requests.
    I'm not active on the forum anymore. Please contact other forum staff.
    Ed05
    Posts: 556
    Reputation: 7

    Message # 185 | 2:37 PM
    How to hide some things from some modules with this. I have try with most codes and it wont work. I want to hide chat box for certain module. !

    Huh i resloved with this <?if($MODULE_ID$="dir")?>No code <?else?> code<?endif?>

    !
    Post edited by Ed05 - Friday, 2014-07-18, 2:41 PM
    Urs
    Posts: 303
    Reputation: 4

    Message # 186 | 4:28 PM
    If a conditional operator is used :

    Code
    <?if($URI_ID$='page200')?>
    <title>name</title>
    <meta name="description" content="about name" />
    <meta name="keywords" content="name, about, about name" />
    <?endif?>


    Then the general code that was applied to all "site pages" :
    Code
    <title>$SITE_NAME$ - $MODULE_NAME$</title>


    Must be removed and then you need a conditional operator for every single page ?

    Otherwise the first version will overwrite the conditional operator.

    Wouldn't it be better if i could use a single title / meta-description / meta-keywords to automatically produce the page information without writing conditional operators for every single page ?

    For the title i have this :
    Code
    <title>$SITE_NAME$ - $MODULE_NAME$</title>


    How to write a Meta description and meta-keywords that automatically adjusts like the title depending on page ? Is it possible ?

    <?$META_DESCRIPTION$?>

    Thassos Island Portal :
    https://thassos.one
    Post edited by Urs - Friday, 2014-08-08, 5:07 PM
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 187 | 9:33 AM
    Quote Urs ()
    Must be removed and then you need a conditional operator for every single page ?


    It is not necessary to remove the default code for title. In your example, if it is page200 - its specific title/meta tags will be displayed, if it is any other page - the default title will be displayed. In other words, the default title will be displayed on all pages where their specific titles are not set. The default title will not overwrite your conditions.
    Quote Urs ()
    How to write a Meta description and meta-keywords that automatically adjusts like the title depending on page ? Is it possible ?


    It is possible in content modules - you can use additional fields for meta tags, and then use them in the code, e.g.

    Code
    <meta name="description" content="$OTHER1$" />  
    <meta name="keywords" content="$OTHER2$" />


    where $OTHER1$, $OTHER2$ are the codes of the additional fields.

    I'm not active on the forum anymore. Please contact other forum staff.
    Urs
    Posts: 303
    Reputation: 4

    Message # 188 | 5:07 PM
    <title>$SITE_NAME$ - $MODULE_NAME$</title> overwrites any conditional operator. (title)

    It would have been better if <title>$SITE_NAME$ - $MODULE_NAME$</title> applied to all pages except for the ones that use conditional operators.

    So that the conditional operator would overwrite <title>$SITE_NAME$ - $MODULE_NAME$</title> and not the other way around.

    Thassos Island Portal :
    https://thassos.one
    Post edited by Urs - Monday, 2014-08-11, 5:07 PM
    Good
    Posts: 365
    Reputation: 12

    Message # 189 | 10:56 AM
    Hi All,

    It seems len function can not be used with <?if condition.

    For example,
    Code

    <?if(len(S)=100)?>A<?else?>B<?endif?>


    This function does not work though how hard I try. It seems <?if?> condition only work with expression defined by uCoz.

    Please help to on this case!
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 190 | 2:49 PM
    Good, it can be used, e.g.

    Code
    <?if(len($TITLE$)>50)?>blabla<?endif?>

    I'm not active on the forum anymore. Please contact other forum staff.
    joexyz
    JOE-vascript
    Posts: 1770
    Reputation: 78

    Message # 191 | 4:04 PM
    Good, you can use substrings like this:
    Code
    <?if(len($TITLE$)>50)?><?substr($TITLE$,0,50)?>...<?else?>$_SIGNATURE$<?endif?>

    This code will show a short version of title, if the title's length is more than 50 characters. E.g. for this title:

    Quote Original title
    The title of this long article is more than 50 characters, so it'll be shortened.


    The code will return:

    Quote Returned title
    The title of this long article is more than 50 ch...


    If the title is short, the code returns $TITLE$ without three points at the end (...).

    Quote
    Short title

    hey i'm joe and i do not work for the company anymore, please contact other staff or tech support
    icon by ch-chau

    sometimes i lurk here
    Good
    Posts: 365
    Reputation: 12

    Message # 192 | 2:42 AM
    Hi Sunny and bigblog,

    How about this expression? Could you please help to write in code?

    If len(S) > 164 and len(S) < 166, then substr (S,2,2) endif

    thank you~~

    Added (2014-09-09, 8:42 PM)
    ---------------------------------------------
    I have found out the solution. Please ignore this message. Thanks for your support. smile smile

    Mar1aN
    Posts: 192
    Reputation: 3

    Message # 193 | 4:09 PM
    Is there a uCoz conditional that can round numbers?

    Example: http://php.net/manual/ro/function.round.php .

    Sunny
    Posts: 9296
    Reputation: 456

    Message # 194 | 9:54 AM
    Mar1aN, the int parameter can do it: http://screencast.com/t/InzTItuk . Example:

    Code
    <?int($ORDER_AMOUNT_RAW$)?>

    I'm not active on the forum anymore. Please contact other forum staff.
    Mar1aN
    Posts: 192
    Reputation: 3

    Message # 195 | 7:39 PM
    Search: