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.
  • SirDarknight
    Posts: 247
    Reputation: -5

    Message # 76 | 12:37 PM
    And If I change the online games to file catalog? That User needs to make 5 Posts before downloading any thing?

    Added (2011-01-22, 6:37 Am)
    ---------------------------------------------
    When I used Ctrl+F to search for $CALENDAR$ It says item not found

    Animorph
    Posts: 2856
    Reputation: 189

    Message # 77 | 1:18 PM
    Quote (SirDarknight)
    And If I change the online games to file catalog? That User needs to make 5 Posts before downloading any thing?

    just use conditional operators like you did before but just put the codes of file catalog in it

    Quote (SirDarknight)
    When I used Ctrl+F to search for $CALENDAR$ It says item not found

    is normally located in blocks look if you have 2 $CALENDAR$ codes there or not , also if there is only 1 code in blocks search on other places (templates) for a second code , i'm not god i can't guess where you used the $CALENDAR$ code twice

    To busy building a passive income online ;)
    SirDarknight
    Posts: 247
    Reputation: -5

    Message # 78 | 8:54 AM
    Code
    <?if(!$USER_LOGGED_IN$)?><script><a href="javascript://" onclick="new _uWnd('myName','Welcome',500,200,{min:1,shadow:1,header:1,max:1,resize:0},'Guests are not able to download anything and viewing some forums. <a href="http://www.hugesoft.tk/index/3">Register Here</a> and Download Anything ');"></a></script><?endif?>

    Why It doesn't work?

    I want when Guests will access a pop up will appear and tell some text.

    Added (2011-01-29, 2:52 Am)
    ---------------------------------------------
    But It works

    Code
    <?if(!$USER_LOGGED_IN$)?><script>
    new _uWnd.alert('Guests are not able to download anything and viewing some forums. <a href="http://www.hugesoft.tk/index/3">Register Here</a> and Download Anything ','Welcome',{w:500,h:100,tm:30000});

    But I want the Minimize and Maximize Button.

    Added (2011-01-29, 2:54 Am)
    ---------------------------------------------

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

    What Does It mean?

    Mark69
    Posts: 17
    Reputation: 0

    Message # 79 | 9:42 PM
    Is it possible to set a conditional operator to load multiple $USERNAME$s from a file on the server(file manager)?
    CodeResolution
    Posts: 1570
    Reputation: 58

    Message # 80 | 9:43 PM
    Mark69, I don't think this is possible.
    Kind regards,
    Elliott.

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

    I'm on Behance and Twitter.
    Animorph
    Posts: 2856
    Reputation: 189

    Message # 81 | 10:13 PM
    Mark69, to confirm CreativeCollusions this is not possible
    To busy building a passive income online ;)
    kliptu
    Posts: 33
    Reputation: 0

    Message # 82 | 1:37 PM
    I like the codes given in the 1st thread of this post related to Hello username or else Hello guest, please register.

    I want to know where do I put the codes in my website? If I have to put it in between <head> and </head> then which part of <head> should I add the code to?

    Please guide me on this matter, I log in to my control panel and then click on design > design management (templates) and I find parts of the template broken down into global blocks, page editor and the modules installed....... now in which part of the template code do I add the code for user's welcome message?

    Natashko
    Posts: 3366
    Reputation: 171

    Message # 83 | 4:18 PM
    kliptu, you need to put it between and in the place where you want it to appear on the website.
    Lt99range
    Posts: 16
    Reputation: 0

    Message # 84 | 11:44 PM
    Is there a link to a list of ALL the operators?

    I'm trying to make it so if an admin is online, text will say so.

    I thought I had to by doing <?if($USERNAME$=='OwnerTravis')?>online<?else>offline<?endif?>

    It appeared to work, but quickly I realized that only I could see it. (my username is OwnerTravis) I'm sure I could figure it out if I knew all the operators available, however I don't know them. I'm sure there is a simple one like $ADMIN_LOGGED_ON$ or something that would make it obvious on how to make this command.

    Any help/suggestions are appreciated, thank you.

    Added (2011-02-23, 5:44 PM)
    ---------------------------------------------
    I'm beginning to think you have to use substr, however I'm trying to get it down but I can't. I got this far
    <?if(substr($ONLINE_USERS_LIST$,0,5)='Owner')?> but obviously it doesn't work other wise I wouldn't be asking. Argh I'm getting closer! I hope

    Post edited by Lt99range - Wednesday, 2011-02-23, 11:03 PM
    CoffeeCone
    Posts: 687
    Reputation: 41

    Message # 85 | 1:59 AM
    Lt99range, if I got your problem correctly, you want to show something the admins am i right? For that you can try the code below.
    Quote
    <?if($GROUP_ID$==GROUPID)?>Some Admin Stuff<?endif?>

    GROUPID is the corresponding ID column found on http://mon3y.ucoz.com/panel/?a=users;l=groups.
    If that's not what you're aiming for, feel free to give more information about what you want to achieve.

    "Friends don't let friends use Internet Explorer 6." - Microsoft || Join the cause. Help your friends.


    Lt99range
    Posts: 16
    Reputation: 0

    Message # 86 | 2:04 AM
    shadowslash, No. I knew how to do that, what I want is when an admin is online, a message says "An admin is online", and if there aren't, it says "all admins are offline"

    Basically this LOGIC:

    <?if($A_ADMIN_IS_ONLINE)?>An admin is online!<?else?>No admins are online.<?endif?>

    Added (2011-02-23, 8:04 PM)
    ---------------------------------------------
    I don't just want ADMINS to see it, I want EVERYONE to be able to see it. If that makes sense. But I want it to only show a different message depending if an admin is online or not.

    If it helps currently I am the only admin, and probably will only ever be the only admin lol.

    Its so users know there is a admin currently available.

    Post edited by Lt99range - Thursday, 2011-02-24, 2:09 AM
    CoffeeCone
    Posts: 687
    Reputation: 41

    Message # 87 | 3:44 AM
    Lt99range, sorry for the late response, I had to cook up the code from scratch but anyway, here you go. It may not be the direct solution to your question but it does let you detect if a user is online or not.

    Here's my new tutorial in the Hints and Tips for uCoz section.
    http://forum.ucoz.com/forum/38-12655-1


    "Friends don't let friends use Internet Explorer 6." - Microsoft || Join the cause. Help your friends.


    HorseIsleSense
    Posts: 93
    Reputation: 0

    Message # 88 | 9:15 PM
    The first post is confusing.. What code do I need if I want guests to see this text:

    "Welcome to TheHelperUnion! As a guest, you cannot view most of our pages. To be able to see our great guides, register using your HorseIsle 2 Username!"

    And for everyone to see other text, including guests?

    CodeResolution
    Posts: 1570
    Reputation: 58

    Message # 89 | 9:20 PM
    HorseIsleSense,
    Code
    <?if($USER_LOGGED_IN$)?>Users logged in see this so change this<?else?>Welcome to TheHelperUnion! As a guest, you cannot view most of our pages. To be able to see our great guides, register using your HorseIsle 2 Username!<?endif?>

    Kind regards,
    Elliott.

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

    I'm on Behance and Twitter.
    Bio
    Posts: 3
    Reputation: 0

    Message # 90 | 6:21 AM
    Conditional code being used in news posts isn't functioning correctly. The code on its own works fine but when I add it to the html source for the news entry it fails sad

    Code

    <?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>

    The code is designed to only allow user how are logged in to download the file.

    Does the if statement just not work in news entries??

    uCoz Community » uCoz Modules » Additional Features » Conditional Operators (<?if($VAR$)?>...<?else?>...<?endif?>)
    Search: