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.
  • Animorph
    Posts: 2856
    Reputation: 189

    Message # 106 | 10:49 PM
    komsha, i checked it myself , it works correctly , please provide your code and information where you want it to work
    To busy building a passive income online ;)
    komsha
    Posts: 77
    Reputation: 1

    Message # 107 | 12:56 PM
    Animorph, Jan, I solved the problem,ty.

    Added (2011-04-26, 6:56 Am)
    ---------------------------------------------
    cann i do next? in publisher>page with entry full text and its comments i try this code

    <?if($CATEGORY_ID$=3 or $CATEGORY_ID$=4)?> my text <?endif?>

    i wannt this text to be only in this two category on publisher,no in other.is it possible?

    Natashko
    Posts: 3366
    Reputation: 171

    Message # 108 | 2:24 PM
    komsha, it will work, but substitute $CATEGORY_ID$ with $CID$
    komsha
    Posts: 77
    Reputation: 1

    Message # 109 | 2:57 PM
    i try to make when user loged in not display pop up,and if ther guest ajax pop up show.how to do that?
    Animorph
    Posts: 2856
    Reputation: 189

    Message # 110 | 9:05 PM
    komsha, you need a script for that , PM me about it and i will provide it to you , its a third party script so i do not recommend it .
    To busy building a passive income online ;)
    Ronzky
    Posts: 56
    Reputation: 0

    Message # 111 | 4:10 PM
    is there conditional operators like <?if($USER_POSTS$='100')?>
    o_lisovenko
    ico.ukit.com
    Posts: 132
    Reputation: 15

    Message # 112 | 5:24 PM
    Ronzky, where do you want to use it? Posts of what module do you mean?
    Ronzky
    Posts: 56
    Reputation: 0

    Message # 113 | 5:29 PM
    forum and site news. I wanna make a code that if users has 100 posts then he/she can view the page. Can I do like that?
    Post edited by Ronzky - Monday, 2011-06-20, 5:29 PM
    o_lisovenko
    ico.ukit.com
    Posts: 132
    Reputation: 15

    Message # 114 | 5:57 PM
    Ronzky, you'll not be able to use this codes in all templates. This codes is working only in "User personal page" template.
    Ronzky
    Posts: 56
    Reputation: 0

    Message # 115 | 6:08 PM
    ok thanks a lot.
    Helpinghand
    Posts: 37
    Reputation: 0

    Message # 116 | 5:12 AM
    How to put this forum link(when click here,direct go site forum) on my site"http://Freeforall.ucoz.com" top below my site banner .
    Im post one other site image:im trying to make same.
    http://i54.tinypic.com/28kk9r8.jpg
    Paradox
    Old Guard
    Posts: 3284
    Reputation: 145

    Message # 117 | 5:31 AM
    Helpinghand, how is this at all related to uCoz's conditional operators? Next time please remember to post your threads in the correct forums.

    As for your issue you need to insert the image under the site banner within a hyperlink. This will most easily be done using the HTML editor mode.

    Hope this helps, happy

    Jack of all trades in development, design, strategy.
    Working as a Support Engineer.
    Been here for 13 years and counting.
    Helpinghand
    Posts: 37
    Reputation: 0

    Message # 118 | 7:42 AM
    sir.Im new here,register yesterday,where put this..im confuse......
    Paradox..Thanks for your sugeetion.....Great site realy helpfull.im trying make this but not succes,can u tell me where find one video tutial,that iam success to make this.pls.......
    Paradox
    Old Guard
    Posts: 3284
    Reputation: 145

    Message # 119 | 7:53 AM
    Helpinghand, okay. You would put this code:

    Quote
    <a href="http://freeforall.ucoz.com/forum"><img src="image link here"></a>


    under the HTML for the page header. You will find the pages HTML by opening Control Panel > Design Customisation > Top part of website. I have answered you in your other thread about the video tutorials.

    Please do try at least to post your threads in the correct forums. Hope this helps, happy

    Jack of all trades in development, design, strategy.
    Working as a Support Engineer.
    Been here for 13 years and counting.
    Helpinghand
    Posts: 37
    Reputation: 0

    Message # 120 | 6:02 PM
    Paradox,Thanks..but not working for me.
    im post my site code below,pls see.........
    My site "top part of the website" full code :

    sir,Im trying to make this type......

    my site banner pic.
    Post edited by Helpinghand - Monday, 2011-06-27, 6:04 PM
    Search: