TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 728
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 377
  • Total: 377

Conditional Block (probably the most useful block in the universe)

Started by akulion, February 01, 2007, 03:10:58 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

akulion

Hi,

This is something I learned about a year back from J.A.CORTINA down at SMF.

Its been repeated several times but it just saddens me to see that most people dont use it since it is a very powerful thing indeed!

Conditional Block

Type of block PHP

Position anywhere depending on content

Code

This is the general code for the block, I will attempt to explain it fully and give some  applications.



global $user_info;

if ($user_info['is_guest'])
echo 'CONTENT FOR GUESTS';

elseif (in_array(#, $user_info['groups']))
echo 'CONTENT FOR MEMBERS SPECIFIED';

else
echo 'CONTENT FOR OTHER MEMBERGROUPS NOT SPECIFIED ABOVE';


Replace the '#' in the 'in_array(#, $user_info['groups'])' with the number of the membergroup you want

Explanation
In the first part of the code goes any code you want which will only be displayed for guests...
example: notice the red register banner and only 3 tabs displayed at the center welcome block? Well remember the layout because it is about to change...

In the 2nd part of the code you specify which membergroups can see what content, so for example you can put a seperate elseif statement for just your admin and mods, and then another one yet for VIP members and then another one for other membergroups.
Example: Login using the username test and password test and see how the whole layout has changed. Remember the red register button? Its gone now! Also there are additional tabs for members and for guests there were only 3

3rd Part concludes the whole conditional statement block and can be used for everyone else who was not directly specified in the above statements. So we talked about guests, and mods and admins and VIP members- so here we put the content for everyone else which ever member group they may be in.
Example: not available because you have to be an admin or mod on my site to notice the difference, however I can just tell you that an additional menu appears for admins with quick links to common admin interface options

The End



akulion

Understanding the Echo Statement

This is a mistake I would often make myself - and the mistake is not getting to know mr. Echo

Echo is your friend in SMF...so we must learn a few things about it.

1. echo basically prints whatever you put inside it on to your file AS IS. So it comes in handy for including scripts and text and other things in your pages.

Try this somewhere in a PHP block in TP
echo 'hello mr echo';
It will out put hello mr echo on your page as plain text

If you had put

echo 'hello <b>mr</b> echo';

The result would have been: hello mr echo
So it also can take html commands

2. Echo statements in SMF are used with single quotes '
EXAMPLE:
echo 'CONTENT FOR GUESTS';

The statement itself concludes(ends) with the ; sign. But the printing is concluded with a ' single quote.

But it is very important to understand one thing about the '
If you put a single quote anywhere in your code which you are echoing out, it will be recognized as a command to stop printing the info!

So if I put:
echo 'hello mr echo how's your wife?';

The above will give me an error, because I put a single quote in the how's
But that dosent mean we cant use any single quotes at all. We can, all we have to do is put a slash befoe it like this:

echo 'hello mr echo how\'s your wife?';
That way the ' will be ignored by your php processor.

This is a useful trick also to know when echoing out Java Script or other scripts which may be using single quotes in them.
All you would have to do is simply escape all the single quotes with a \

Example:

echo'
<a href="http://yoursite.com/mediaplayer/mp3player.html" onMouseOver="stm(Text[21],Style[1])" onMouseOut="htm()"  onclick="window.open(\'http://yoursite.com/mediaplayer/mp3player.html\',\'media\',\'height=405,width=310\'); return false;">
';


notice all the single quotes have been escaped with a \ before them.
Ofcourse the final one marks the end of the echo statement so we dont close that.

The End

PS: Im not some php expert, im just sharing the simple things I have learned :P


Ken.

Great lesson Aku, gave me a little more understanding of the codes we use for SMF & TP.

That thing about the back slash had been in something that I was working on earlier today, now I know what it was trying to do.
" If everything seems under control, you're not going fast enough." - Mario Andretti
Yesterday When I was Young.

Thurnok


iowamf

Thanks for this thread.

Thurnok, I take it you don't use the Menu Manager in TinyPortal or did you modify the source code to apply the conditionals?

Quote from: Thurnok on February 01, 2007, 07:49:42 PM
All my menus on my various sites use conditionals.

Example:


iowamf

Thanks for the input - quick question about quotes (the thread is already off topic) ...

Quote from: Aku on February 01, 2007, 03:15:06 PM
Understanding the Echo Statement

2. Echo statements in SMF are used with single quotes '
EXAMPLE:
echo 'CONTENT FOR GUESTS';


Why not use double quotes? Both of these will work, but the double quotes seems easier:

echo "something entered was: $some_string <br />";
// double quotes vs single quotes usage
echo 'env is '.$env.' action_string is '.$action_string.'<br />';

houston

I seem to be getting this error. Inserted this global $user_info;

if ($user_info['is_guest'])
echo 'CONTENT FOR GUESTS';

elseif (in_array(#, $user_info['groups']))
echo 'CONTENT FOR MEMBERS SPECIFIED';

else
echo 'CONTENT FOR OTHER MEMBERGROUPS NOT SPECIFIED ABOVE';


And got this in a php block.

Parse error: parse error, unexpected T_ECHO, expecting ')' in /home/xxxxx/public_html/newforum/Sources/Load.php(1745) : eval()'d code(35) : eval()'d code on line 7

RoarinRow


SMF 2.0 RC3
TP 1.0 beta 5-1
Wordpress 3.0

JPDeni

houston, you need an actual number instead of the # in


elseif (in_array(#, $user_info['groups']))


Something like


elseif (in_array(1, $user_info['groups']))

This website is proudly hosted on Crocweb Cloud Website Hosting.