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.
  • CodeResolution
    Posts: 1570
    Reputation: 58

    Message # 46 | 4:01 PM
    Oh wacko Im very sorry. Must have missed your post.

    Thankyou very much Sunny smile


    Kind regards,
    Elliott.

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

    I'm on Behance and Twitter.
    AlShaw
    Posts: 4
    Reputation: 0

    Message # 47 | 10:56 PM
    Is there a way to do a conditional operation based on the browser the person is using?

    e.g People without Firefox get a pop-up message saying "This site is best viewed with Mozilla Firefox."

    If so, what is the code?

    Dartz
    Posts: 1931

    Message # 48 | 11:01 PM
    AlShaw, don't be lazy and read thread's content:

    http://forum.ucoz.com/forum/37-471-49339-16-1263328788


    [... With uCoz since December 2 2007 ...]
    AlShaw
    Posts: 4
    Reputation: 0

    Message # 49 | 11:23 PM
    Sorry, you don't have to be so snappy. I just don't have the time to read the whole thread.
    Dartz
    Posts: 1931

    Message # 50 | 11:35 PM
    Quote (AlShaw)
    I just don't have the time to read the whole thread.

    And I have time to babysit each lazy user? - I have to answer whole forum because
    on weekends I am only one doing that. I wont go and pat you on shoulder saying: -

    - "Don't worry, dear, I'll feed you with a link".

    It's not hard to hit F3 and enter keyword on 4 pages. This thread doesn't have 20.


    [... With uCoz since December 2 2007 ...]
    Thunderword
    Posts: 19
    Reputation: 0

    Message # 51 | 8:28 AM
    is it corect?

    Code
    <?if($USER_LOGGED_IN$) and ($ADMIN_LOGGED_IN$)?>ADMIN is here!!!<?endif?>

    i want to show, if there is an admin logged in, that there is an admin logged in :P

    Dartz
    Posts: 1931

    Message # 52 | 10:20 AM
    No, http://forum.ucoz.com/forum/5-10131-1

    And for some reason I got feeling you two are same person.


    [... With uCoz since December 2 2007 ...]
    Thunderword
    Posts: 19
    Reputation: 0

    Message # 53 | 4:00 PM
    i want to put smth in the tagboard thing...
    so...
    it's smth like this
    Code
    <?if($USERNAME$)?> <?if($GROUP_ID$=3)?> MOD <?else?> <?if($GROUP_ID$=4)?> ADMIN <?endif?> <?endif?> <?endif?></div>

    it doesn't really work
    what i want is: if there is an admin posting the message, there will be an admin at the begining of the post...
    or if it's an mod posting...
    well...
    the same thing..
    can sumbody chane that code into what i want?
    or is it possible or not?
    cause i don't want to bother for nothing...

    P.S.: i just want to know if it is possible or not... not comments like "Try it yourself"... cuz i tried!

    Dartz
    Posts: 1931

    Message # 54 | 4:36 PM
    It is possible but we do not teach coding. If you can't manage this on your
    own then you must accept fact your website wont have this type of system.

    [... With uCoz since December 2 2007 ...]
    Thunderword
    Posts: 19
    Reputation: 0

    Message # 55 | 2:24 PM
    oke...
    thanks biggrin
    Mkiller
    Posts: 41
    Reputation: 0

    Message # 56 | 7:44 PM
    If i want groups colors in userlastPost and userBirthdayLink by this codes

    if user admin - userBirthdayLink be red..

    <?if($GROUP_ID$=3)?>userBirthdayLink {color:red}<?endif?>

    can you correct that code for me?
    And where to post it?
    Can give me some guide?

    Added (2010-05-22, 1:44 PM)
    ---------------------------------------------
    Can some one explain form me, how to make it??

    i dont know how to make it, and where to post it...

    <?if($GROUP_ID$=1?> a.forumLastPostLink {color:#9E6EFF;}<?endif?>

    Post edited by Mkiller - Thursday, 2010-05-20, 5:39 PM
    Dartz
    Posts: 1931

    Message # 57 | 9:26 PM
    I'll look into this matter Sunday. Please be patient.
    [... With uCoz since December 2 2007 ...]
    Mkiller
    Posts: 41
    Reputation: 0

    Message # 58 | 2:46 PM
    Okay, thanks Dartz
    i will wait biggrin

    Added (2010-06-02, 8:46 Am)
    ---------------------------------------------
    Hi Dartz,
    any news?

    websiteguru
    Posts: 16
    Reputation: 0

    Message # 59 | 9:03 PM
    Can I do something like this:

    <?if($URL$='http://www.example.com/forum/')?>

    I want to do something like that so that the content that is inside those tags only shows on that website address. I don't want to do something like...

    <?if($MODULE_NAME$='forum')?>

    ...because I don't want it to show up on all forum pages.

    Thanks in advance. biggrin

    Added (2010-07-30, 3:03 PM)
    ---------------------------------------------
    Nevermind, maybe I should read before I post, lol, too lazy. happy

    I went to list of page variables and did <?if($URI_ID$='frMain')?>

    Zain
    Posts: 68
    Reputation: -7

    Message # 60 | 7:30 AM
    i use the following in comments for showing group icon of users in comment. but it displaying the group icon of users that enter to site in every comment. i also use $GROUP_ID$ $GROUP_ICON$ but it's not work too. how to show group icon in comment.

    Code
    <?if($CUR_GROUP_ID$=1)?><img src="http://dl4uall.com/images/user.png" alt="User" border="0"><?endif?><?if($CUR_GROUP_ID$=2)?><img src="http://dl4uall.com/images/author.png" alt="Author" border="0"><?endif?><?if($CUR_GROUP_ID$=3)?><img src="http://dl4uall.com/images/mod.png" alt="Moderator" border="0"><?endif?><?if($CUR_GROUP_ID$=4)?><img src="http://dl4uall.com/images/admin.png" alt="Administrator" border="0"><?endif?><?if($CUR_GROUP_ID$=251)?><img src="http://dl4uall.com/images/regular.png" alt="Regular" border="0"><?endif?><?if($CUR_GROUP_ID$=255)?><img src="http://dl4uall/images/guest.png" alt="Guest" border="0"><?endif?>
    Search: