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 # 166 | 3:01 AM
    Im interesed how to add special conditition for hot enterises to mark them hot, i mean most viewed .. i try with this
    Code
    <?if($READS$="1000")?><img src="some image " border="0" align="absmiddle" alt="wink" /> "><?endif?>
    But nothing apears happy

    !
    Post edited by Ed05 - Sunday, 2013-11-03, 3:02 AM
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 167 | 12:14 PM
    Ed05, the correct code will be

    Code
    <?if($READS$>"1000")?><img src="some image " border="0" align="absmiddle" alt="wink"><?endif?>

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

    Message # 168 | 3:33 PM
    Sunny, awesome smile tnx
    !
    Muşatinu
    Posts: 46
    Reputation: 0

    Message # 169 | 9:35 PM
    Hi,

    I use /stuff module and i want to make something that shows the entries only for the authors ( person who added the entries ).

    I use the following condition:

    Code
    <?if($USERNAME$=$USERNAME$)?>
    Appeareance of entries
    <?endif?>


    But didn't work. Is there any chanse to make something like that?

    Thanks.
    khen
    Posts: 475
    Reputation: 13

    Message # 170 | 8:30 AM
    Muşatinu, try to use this code:

    Code

    <?if($USERNAME$=$USERNAME$)?>
    Appeareance of entries   
    <?else?><?endif?>


    or

    you may group your authors by using $GROUP_ID$

    Post edited by khen - Friday, 2014-01-03, 8:30 AM
    Paradox
    Old Guard
    Posts: 3284
    Reputation: 145

    Message # 171 | 9:19 AM
    Muşatinu, khen, guys, at the moment you're both comparing only the same string. You need to determine the $VAR$ that specifies the authors username. It is generally $USER$ or something like that. I will have a look at this module and get back to you later today. smile
    Jack of all trades in development, design, strategy.
    Working as a Support Engineer.
    Been here for 13 years and counting.
    NormalX
    Posts: 94
    Reputation: 7

    Message # 172 | 4:53 PM
    " is this supported <?if($DAY$>=12)?> , greater than or equal to??? " Originally.

    I tried and it worked, so it is...Never Mind!
    Post edited by NormalX - Friday, 2014-01-31, 4:59 PM
    Ed05
    Posts: 556
    Reputation: 7

    Message # 173 | 5:49 AM
    which conditional operator i have to use for option to see comments from other users on my files,enteries?
    !
    Animorph
    Posts: 2856
    Reputation: 189

    Message # 174 | 7:34 AM
    Ed05, Go to CP --> Informers --> Create --> Section: File Catalog --> Data Type: Entries --> Sorting Mode: Entry Comments A or D (depends , if you want the most recent comments or the most popular.) --> Numer of entries and Numer of columns
    To busy building a passive income online ;)
    Ed05
    Posts: 556
    Reputation: 7

    Message # 175 | 4:21 AM
    Animorph, no no we miss understood, i need notification list if other users comments on my enteries for all modules or modules blog and file katalog if is posibile , i need that for my custom script wich looks like this
    Code
    <img src="http://supercafe.ucoz.com/raznokoD/adminpanelstrelica.png" border="0" aling="absmiddle" width="13"><a href="some link">Comments on my enteries</a> <br>

    And when i click on the link the new tab opens with list.. cry

    Added (2014-02-04, 10:21 PM)
    ---------------------------------------------
    angry


    !
    Animorph
    Posts: 2856
    Reputation: 189

    Message # 176 | 8:45 PM
    Ed05, this option is already made by uCoz.

    When you add an entry checkmark this:



    If you do not have this option go to CP ---> YOUR MODULE ---> Module settings ---> Fields of a new entry form --> Notifications about comments
    Attachments: 4999263.png (13.1 Kb)

    To busy building a passive income online ;)
    Canada_Job
    Posts: 4
    Reputation: 0

    Message # 177 | 12:23 PM
    hello
    do we have any alternative conditions like : '< ',' >' or '=<' because i need to use this for $add_date$.
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 178 | 3:22 PM
    Canada_Job, yes. You can find a detailed description of conditional operators in the Customize Design section: http://screencast.com/t/HFcDO2OZOzf
    I'm not active on the forum anymore. Please contact other forum staff.
    Ed05
    Posts: 556
    Reputation: 7

    Message # 179 | 10:02 PM
    I want to use one module for more things, and when i add enteries that every entry apear on specified place. for example i want to make page for music and page for images in same module and when i add image that image could not show on page wich i specified like music... ?
    That goes with <if? operators but i cant find right one cool

    !
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 180 | 1:59 PM
    Ed05, why don't you simply use different module categories for music and for images? Depending on what exactly you want to implement, you could also use Informers that will display entries only of a certain category.
    I'm not active on the forum anymore. Please contact other forum staff.
    Search: