uCoz Community » uCoz Modules » Additional Features » Conditional Operators (<?if($VAR$)?>...<?else?>...<?endif?>)
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.
  • Gigabyte
    Posts: 25
    Reputation: 2

    Message # 91 | 8:27 AM
    Unfortunately, these codes do not work in news entries, load entries, etc.

    You also two unnecessary <div> tags in there.

    Quote
    <?if($USER_LOGGED_IN$)?>
    <div style="text-align: center;"><span style="font-size: 14pt; ">Download Here</span><div><a href="http://www.webserver.com/file.exe"><img src="http://www.webserver.com/image.jpg"></a></div><?else?><div align="center">Sign up now to download this file!</div><?endif?> </div>

    Gigabyte Zone - [ ... One Family, One Goal; Making Your Life A "Byte" Easier! ... ]
    Movies, Games, Applications, Music, Books and plenty more!
    Natashko
    Posts: 3366
    Reputation: 171

    Message # 92 | 8:57 AM
    Bio,
    Quote (Bio)
    The code on its own works fine but when I add it to the html source for the news entry it fails

    if I understood you correctly, you add the provided code, when adding entries. But this code should be changed in the template itself. CP->Design->Design management(templates)->Site news->Page with entry full text and its comments
    Quote (Gigabyte)
    Unfortunately, these codes do not work in news entries, load entries, etc.

    It does work for news entries.
    If we speak about Site news in particular, I suggest you use several custom fields for adding download links (CP->Site news->Module settings) And then set a condition for users not to be able to view those fields.
    BigInNYC
    Posts: 3
    Reputation: 0

    Message # 93 | 7:02 PM
    I need help. I would like to allow people to select there own theme other than the default theme. How would I go about that using Conditional Operators. If it can be done at all. happy
    Animorph
    Posts: 2856
    Reputation: 189

    Message # 94 | 7:20 PM
    BigInNYC, it can be done , i answered you in your previous thread about how much work it will cost for you
    To busy building a passive income online ;)
    Natashko
    Posts: 3366
    Reputation: 171

    Message # 95 | 12:41 PM
    BigInNYC, there is no way we can make a condition for you. But it has to look like this:
    the first template or the style file the second template or the style file
    donyusufahmed
    Posts: 17
    Reputation: 0

    Message # 96 | 1:22 AM
    Hi, i am wondering if i can do this with conditional operators
    On the forum page, i created a download section. But i want people who have made specific number of posts to be able to access the contents within that page.
    Here are pictures.
    I created this
    http://s661.photobucket.com/albums/uu334/donyusufahmed/?action=view¤t=why.jpg

    When they click "download,"
    i want them to have at least 10 posts before they can see the link in this
    http://www.imagedoll.com/show.php/21713_thread.jpg.html

    Animorph
    Posts: 2856
    Reputation: 189

    Message # 97 | 1:30 AM
    This is not something that you do with conditional operators , this can be solved with Auto Transfer from User to Checked group feature that you can edit in your control panel

    go to CP ----> Users ---> User Group ---> Users [Auto Transfer]
    - Select the number of post that the user needs to get to get automaticly transfered to Checked group and press save.

    now go to CP ---> Forum ---> Management of forums and sections ---> your board name (Download) ---> Edit icon ---> Forum entries may be read by: ---> Uncheck the Group named Users and then press save.

    This will make the registered users from your website that are still in the group below 10 post for example not able to acces the board


    To busy building a passive income online ;)
    donyusufahmed
    Posts: 17
    Reputation: 0

    Message # 98 | 1:53 AM
    I just did that but that download section just disappeared.
    I want them to be able to see it but not the link in that section unless they have 10 posts
    donyusufahmed
    Posts: 17
    Reputation: 0

    Message # 99 | 3:34 PM
    Heres what its showing now
    Attachments: 9618937.jpg (32.9 Kb)
    Post edited by donyusufahmed - Sunday, 2011-04-17, 3:37 PM
    komsha
    Posts: 77
    Reputation: 1

    Message # 100 | 11:40 PM
    how to make to show avatar when user loged in?somthing like this but doesn't work...
    <?if($USER_LOGGED_IN$)?> $AVATAR$ <?else?>Please, register.<?endif?>
    Animorph
    Posts: 2856
    Reputation: 189

    Message # 101 | 11:43 PM
    komsha, This for example. happy

    Code
    <?if($USER_LOGGED_IN$)?><?if($USER_AVATAR_URL$)?><IMG SRC="$USER_AVATAR_URL$" BORDER="0"><?else?> <IMG SRC="/noavatar.gif" BORDER="0"> <?endif?><?else?>Please, register.<?endif?>

    donyusufahmed, I will re-check your problem with conditional operators on my site , but i don't think that it will work in forum posts


    To busy building a passive income online ;)
    donyusufahmed
    Posts: 17
    Reputation: 0

    Message # 102 | 1:47 PM
    Quote (Animorph)
    donyusufahmed, I will re-check your problem with conditional operators on my site , but i don't think that it will work in forum posts


    okay. thank you very much.
    komsha
    Posts: 77
    Reputation: 1

    Message # 103 | 8:05 PM
    in file catalog>page with entry full text and its comments i try this.

    Code
    <?if($USER_LOGGED_IN$)?>text1<?else?>text2<?endif?>

    but guest don't see text 2.why ?

    Jan
    Posts: 301
    Reputation: 19

    Message # 104 | 9:44 PM
    komsha, code itself works. please be more specific ..
    komsha
    Posts: 77
    Reputation: 1

    Message # 105 | 9:50 PM
    i m added this code in cp>design cust>file catalog>page with entry full text and its comments

    when i m loged in i see text1,but when i m log out i don't see text 2

    i nead,when i m in to see text1,and guest see text 2.

    Post edited by komsha - Friday, 2011-04-22, 9:51 PM
    uCoz Community » uCoz Modules » Additional Features » Conditional Operators (<?if($VAR$)?>...<?else?>...<?endif?>)
    Search: