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.
  • redhead
    Posts: 113
    Reputation: 9

    Message # 151 | 4:12 PM
    Gest3r,
    Quote (Gest3r)
    I need code that will order posts that are only in my specific category.
    Unfortunately, it is not possible by means of standard system solutions.
    Quote (Gest3r)
    Another thing that i need:
    date the News were published)?> News are old News are fresh
    You should use conditional operators and system dollar codes.
    Ookami
    Posts: 31
    Reputation: 0

    Message # 152 | 10:21 AM
    Please help. I have been trying to make my comments display in a way that
    If a guest comments it will display Guest or a name they entered & a guest image.

    so far it just goes straight to the guest image & displays even if i sign in.

    Code
    <?if($USER_LOGGED_IN$)?>
    <div class="eTitle"><a href="$ENTRY_URL$"><font size="1" style="font-size: 8pt; ">$ENTRY_TITLE$</font></a></div><div class="cMessage"><img src="$USER_AVATAR_URL$" width="64" height="64" /><br><b>$USERNAME$:</b> $MESSAGE$</br></div>    

    <?else?>   
    <div class="eTitle"><a href="$ENTRY_URL$"><font size="1" style="font-size: 8pt; ">$ENTRY_TITLE$</font></a></div><div class="cMessage"><img src="guest.gif" width="64" height="64" /><br><b>$NAME$(Guest):</b> $MESSAGE$</br></div>   
    <?endif?>


    now if i code it this way it works fine it displays their usernames & avatars but if a guest comments no username is displayed & avatar is blank.

    Code
    <div class="eTitle"><a href="$ENTRY_URL$">
    <font size="1" style="font-size: 8pt; ">$ENTRY_TITLE$</font></a></div><div class="cMessage"><img src="$USER_AVATAR_URL$" width="64" height="64" /><br><b>$USERNAME$:</b> $MESSAGE$</br></div>
    Post edited by Ookami - Wednesday, 2012-02-08, 10:22 AM
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 153 | 12:58 PM
    Ookami, here are the codes (for the 'Appearance of comments' template):

    for usernames:

    Code
    <?if($USERNAME$)?><a href="$PROFILE_URL$">[b]$USERNAME$[/b]</a><?else?>[b]$NAME$[/b]<?endif?>


    for avatars:

    Code
    <?if($USER_AVATAR_URL$)?><a href="$PROFILE_URL$" title="$USERNAME$" style="padding-right:4px;"><img alt="" align="left" src="$USER_AVATAR_URL$" width="30" border="0" /></a><?else?><img alt="" align="left" src=LINL_TO_DEFAULT_IMAGE" width="30" border="0" /><?endif?>

    I'm not active on the forum anymore. Please contact other forum staff.
    Ookami
    Posts: 31
    Reputation: 0

    Message # 154 | 10:24 PM
    ty
    Mark69
    Posts: 17
    Reputation: 0

    Message # 155 | 10:10 PM
    I was wondering, is there a way to see how many days a user has been registered for?
    I know there's $USER_REG_DAYS$ but that shows the value to the user seeing that code.

    For example, there's a $USERNAME$ and $_USERNAME$ code.
    The first one shows the username of the person viewing the code, the other one shows the username of a user on his/her profile page.

    Is there some variation for the $USER_REG_DAYS$ code that does this?
    Sunny
    Posts: 9296
    Reputation: 456

    Message # 156 | 1:01 PM
    Mark69, I am afraid there is no variable for this purpose.
    I'm not active on the forum anymore. Please contact other forum staff.
    bodbreed
    Posts: 313
    Reputation: 1

    Message # 157 | 4:55 AM
    When I use this code it shows to broken images,
    <div align="center"><?if($IMAGE1$)?><img src="$IMG_URL1$" height="250px" width="250px"><?else?><img src="<div align="center"><!--?if($IMAGE1$)?--><img src="http://driplux.ucoz.com/dl-logo.png"><?endif?></div>

    Added (2012-12-31, 10:55 PM)
    ---------------------------------------------
    Sorry gave you wrong code, here's the right one,

    <div align="center"><?if($IMAGE1$)?><img src="$IMG_URL1$" height="250px" width="250px"><?else?><img src="http://driplux.ucoz.com/dl-logo.png"><?endif?></div>
    mandy
    Posts: 1
    Reputation: 0

    Message # 158 | 4:51 PM
    hey i have one question in the paid services when get money we want transfere money in my bank account

    Added (2013-02-03, 10:51 AM)
    ---------------------------------------------
    please reply me past
    cry

    Sunny
    Posts: 9296
    Reputation: 456

    Message # 159 | 10:50 AM
    mandy, please describe the problem in more detail. What money do you want to be transferred to your bank account? Or do you want to pay from your bank account?
    I'm not active on the forum anymore. Please contact other forum staff.
    bodbreed
    Posts: 313
    Reputation: 1

    Message # 160 | 6:13 PM
    Is it possible to use if statements in meta description?
    cooltaha
    Posts: 57
    Reputation: 6

    Message # 161 | 8:46 PM
    XO-Ferg, yes you can use if statements anywhere
    bodbreed
    Posts: 313
    Reputation: 1

    Message # 162 | 7:01 PM
    I still don't know why this shows an broken image instead of no image when $IMG_URL1$ is not filled in/uploaded...
    Code
    <?if($IMAGE1$)?><img src="$IMG_URL1$" height="250px" width="250px">><?endif?>


    Here's link to entry: http://bit.ly/WnEaaZ
    Post edited by XO-Ferg - Saturday, 2013-03-02, 7:02 PM
    cooltaha
    Posts: 57
    Reputation: 6

    Message # 163 | 7:12 PM
    maybe the reason is <img /> isn't closed the proper way try this

    Code
    <?if($IMAGE1$)?><img src="$IMG_URL1$" height="250px" width="250px" /><?endif?>

    bodbreed
    Posts: 313
    Reputation: 1

    Message # 164 | 7:25 PM
    Quote (cooltaha)
    <?if($IMAGE1$)?><img src="$IMG_URL1$" height="250px" width="250px" /><?endif?>


    Still shows broken image, thanks though.

    Added (2013-03-02, 1:24 PM)
    ---------------------------------------------
    Edit: Wait nevermind, it works now; i put it in the wrong place. Thanks a lot!

    Added (2013-03-02, 1:25 PM)
    ---------------------------------------------
    Edit: Now it doesn't show it when it is filled in.. wink

    cooltaha
    Posts: 57
    Reputation: 6

    Message # 165 | 7:29 PM
    well, from my browser every thing looks great i don't see any broken images try to empty your cache and try again this might do the trick smile
    uCoz Community » uCoz Modules » Additional Features » Conditional Operators (<?if($VAR$)?>...<?else?>...<?endif?>)
    Search: