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.
  • kshengelia
    Posts: 102
    Reputation: 6

    Message # 31 | 8:39 PM
    Has uCoz the <?if?> code for IE? I mean, the information will be visible for user, which enters my site with IE.

    Georgian Mountain Dog (Tarti) named "Balta" - On the avatar
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 32 | 10:01 AM
    kshengelia, explore the available codes, you need the code $USER_AGENT$
    I'm not active on the forum anymore. Please contact other forum staff.
    kshengelia
    Posts: 102
    Reputation: 6

    Message # 33 | 10:22 AM
    Quote (Sunny)
    shengelia, explore the available codes, you need the code $USER_AGENT$

    And can I use something like this <?if($USER_AGENT$=$IE$)?>something<?endif?>


    Georgian Mountain Dog (Tarti) named "Balta" - On the avatar
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 34 | 1:13 PM
    kshengelia,

    Code
    <?if($USER_AGENT$='ie')?>something<?endif?>

    I'm not active on the forum anymore. Please contact other forum staff.
    kshengelia
    Posts: 102
    Reputation: 6

    Message # 35 | 12:48 PM
    Can I use something like <?if($GROUP_ID$=4$)?> for Category Id?

    Georgian Mountain Dog (Tarti) named "Balta" - On the avatar
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 36 | 2:32 PM
    kshengelia, <?if($CID$=4)?>

    But make sure that the code $CID$ is available in the corresponding template.


    I'm not active on the forum anymore. Please contact other forum staff.
    NTP
    Posts: 296
    Reputation: 10

    Message # 37 | 2:12 AM
    no it has to end with <?endif?> <?else?> is by means if something doesn't apply to the pre written text, it will affect them. such as
    Code
    <?if($USER_GROUP$)='Admin'?>Read the pm I sent you<?endif?>
    hence admins will see a message stating the text, while mods won't. read conditional operators http://forum.ucoz.com/forum/37-471-1

    Web design and film == my 2 passions.

    Genesis Inc. ~ New Horizons, New Beginnings
    Post edited by NTP - Friday, 2010-01-15, 2:17 AM
    CodeResolution
    Posts: 1570
    Reputation: 58

    Message # 38 | 11:54 AM
    Hey, sorry but ive tried as much ways as I can think of and I realy need this code.
    Im using my own custom design, and I realy need Section ID Operators for the design.

    Is there any way I can use conditional operators for different sections in a module (for example: Publisher).

    This is an example of what I mean:
    <?if($SECTION_NAME$_ID$='1')?>yes<?else?><?if($SECTION_NAME$_ID$='2')?>no<?endif?><?endif?>

    Anyway, what I mean is, I have a head menu which highlights the active page.
    But I am having to use Publisher for different things, so ive placed different catagories in different sections, and ive put the section links on the menu.
    But, I need the active page link to highlight on the menu. But because there different sections for different stuff each section menu highlight would be different.

    I know hwo to make each link active, but I cant spread each section without a conditional operator.

    The screenshot below should explain better:
    http://forum.ucoz.com/_fr/4/6393095.png

    If you know the conditonal operator for different section ids in the module, please help smile

    Also the $PAGE_ID$ doesnt seem to be working.
    Ive tried:
    <?if($PAGE_ID$='page-name')?>
    <?if($PAGE_ID$='1')?>

    wacko

    Attachments: 6393095.png (20.2 Kb)

    Kind regards,
    Elliott.

    "The best uCoz" critic since 2007.
    Qualified website, branding, print & user interface designer.

    I'm on Behance and Twitter.
    Post edited by Mistory - Sunday, 2010-01-31, 12:49 PM
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 39 | 2:38 PM
    Mistory, try to use $SECTION_NAME$ - <?if($SECTION_NAME$='reviews')?> - or$URI_ID$
    I'm not active on the forum anymore. Please contact other forum staff.
    CodeResolution
    Posts: 1570
    Reputation: 58

    Message # 40 | 11:57 PM
    Thanks for response Sunny, il give it ago.

    Added (2010-02-04, 5:57 Pm)
    ---------------------------------------------
    Sunny, $PAGE_ID$ still doesnt seem to work wacko

    Please help smile


    Kind regards,
    Elliott.

    "The best uCoz" critic since 2007.
    Qualified website, branding, print & user interface designer.

    I'm on Behance and Twitter.
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 41 | 9:55 AM
    Mistory, make sure it is available in the template where you use it, make sure the code is correct.
    I'm not active on the forum anymore. Please contact other forum staff.
    CodeResolution
    Posts: 1570
    Reputation: 58

    Message # 42 | 7:07 AM
    Im using the code exactly how it is displayed in the Conditional Operators example in Custoize Design -> Site Pages (and thats were im trying to use)


    My main page is called exactly 'Homepage'.
    And by according to the example I should have to use: <?if($PAGE_ID$='Homepage')?> or if no capitals: <?if($PAGE_ID$='homepage')?>

    But neither is working. Ivs also tried: <?if($PAGE_ID$='1')?>

    but nothing wacko

    Added (2010-02-08, 1:07 Am)
    ---------------------------------------------
    So is it just an error?


    Kind regards,
    Elliott.

    "The best uCoz" critic since 2007.
    Qualified website, branding, print & user interface designer.

    I'm on Behance and Twitter.
    Post edited by Mistory - Friday, 2010-02-05, 2:26 PM
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 43 | 3:03 PM
    Mistory, $PAGE_ID$ should be taken from the list of page variables (on Admin Bar). Show me the code you are trying to use.
    I'm not active on the forum anymore. Please contact other forum staff.
    CodeResolution
    Posts: 1570
    Reputation: 58

    Message # 44 | 7:45 PM
    No matter, kostova shown me. I guess Dartz may of deleted the post dry

    This is the code kostova had given me and it works: <?if($PAGE_ID$='sitePage1')?>

    Thanks anyway Sunny


    Edited:
    http://forum.ucoz.com/forum/37-471-50906-16-1265731312


    Kind regards,
    Elliott.

    "The best uCoz" critic since 2007.
    Qualified website, branding, print & user interface designer.

    I'm on Behance and Twitter.
    Post edited by Mistory - Tuesday, 2010-02-09, 4:03 PM
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 45 | 9:00 AM
    Mistory, this is exactly what I told:

    Quote (Sunny)
    $PAGE_ID$ should be taken from the list of page variables (on Admin Bar)

    I'm not active on the forum anymore. Please contact other forum staff.
    Search: