If Else If Else

Started by lordtron, July 30, 2008, 12:56:17 AM

Previous topic - Next topic

lordtron

Is it possible to have an If/Else Statement in a If/Else Statement, if you can then how? If not, then how would to work around this?



I want to do this:
If your a quest you see menu1
If your logged in(normal user) you see menu2
If your in membergroup=9 you see menu3
If your an admin then you see menu4



Now, is that possible?

VainSoftGames.com - New Design To Gaming

[SiNaN]

// Is he a guest?
if($context['is_guest']) {

// Then menu1
$menu = 'menu1';
}
// Nope, he is logged.
else {

// Show him menu2 then
$menu = 'menu2';

// Stop, may be he is in group 9
if(in_array(9, $user_info['groups']))
$menu = 'menu3';

// Chnage it to admin menu if he is admin
if($context['allow_admin'])
$menu = 'menu4';
}


$context and $user_info variables must be in globals.
Former SMF Core Developer | My Mods | SimplePortal

lordtron

Now what would I do if I wanted it to be something different, like something that doesn't have a variable.

Like, my homepage(non-forum page) What would I change $menu to for that to work?

VainSoftGames.com - New Design To Gaming

[SiNaN]

What do you mean by "Like, my homepage(non-forum page) What would I change $menu to for that to work?"?
Former SMF Core Developer | My Mods | SimplePortal

lordtron

Well on my homepage for 1 of my websites I have content on it, but I only want to show it to guests(content1) and normal logged(content2) in members. But if you are a member in group9(content3) then your homepage will change to something different and same goes for the admin(content4).

So what would I change $menu to for this to work?

VainSoftGames.com - New Design To Gaming

ccbtimewiz

Something along the lines of:

if [user is logged and not in group #, #, and #]
echo stuff

elseif [user is admin or something]
echo stuff

else [user is guest]
echo stuff

lordtron

Oh, so something like this

3. <?php
4. 
if(in_array(9$user_info['groups'])){
5. echo 'Im in Group 9';}
6. 
7. 
elseif($context['allow_admin']{
8. echo 'Im an Admin';}
9. 
10. 
else($context['is_guest']) {
11.echo 'Im a guest';}
12. ?>



Well that didn't work, I get a syntax error.
Parse error: parse error, unexpected T_ECHO in /call.php on line 8

VainSoftGames.com - New Design To Gaming

[SiNaN]

This is the menu thing that echos your menu.

// Is he a guest?
if($context['is_guest']) {

// Then menu1
$menu = 'my_template_menu1';
}
// Nope, he is logged.
else {

// Show him menu2 then
$menu = 'my_template_menu2';

// Stop, may be he is in group 9
if(in_array(9, $user_info['groups']))
$menu = 'my_template_menu3';

// Chnage it to admin menu if he is admin
if($context['allow_admin'])
$menu = 'my_template_menu4';
}
$menu();


And these are your content.

function my_template_menu1()
{
content for 1
}
function my_template_menu2()
{
content for 2
}
function my_template_menu3()
{
content for 3
}
function my_template_menu4()
{
content for 4
}
Former SMF Core Developer | My Mods | SimplePortal

lordtron

My page goes blank if I use that code.

VainSoftGames.com - New Design To Gaming

[SiNaN]

#9
Did you change the content?




Edit:

Sorry.

if($context['is_guest']) {

Should be

if($context['user']['is_guest']) {
Former SMF Core Developer | My Mods | SimplePortal

lordtron

Yeah, I changed
content for 3
To
'im in a group';

And I changed the others to there correctly saying and it shows nothing.

VainSoftGames.com - New Design To Gaming

ccbtimewiz

Do something like:

$x = array(2, 3);
$y = false;

foreach ($x as $group) {
if( in_array( $group, $user_info['groups'] ) )
$y = true; }

if( $y ) {
echo 'stuff'; }


With the group ID's as the $x variable.

[SiNaN]

lordtron:

Can you show me your file?

ccbtimewiz:

He just needs that for group 9 I guess, just in_array() should be enough.
Former SMF Core Developer | My Mods | SimplePortal

lordtron

Which file, the homepage or the what?

VainSoftGames.com - New Design To Gaming

[SiNaN]

The file that you are using this codes in.
Former SMF Core Developer | My Mods | SimplePortal

lordtron

Alright, there is my file.


VainSoftGames.com - New Design To Gaming

ccbtimewiz

Is $user_info globaled?

[SiNaN]

Didn't understand what this should be doing really?

   'logged user';

Use echo.
Former SMF Core Developer | My Mods | SimplePortal

lordtron

everything in the ' ' is what I want it to show

VainSoftGames.com - New Design To Gaming

[SiNaN]

It should be like this, try. ;)
Former SMF Core Developer | My Mods | SimplePortal

Advertisement: