include() in if...else statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • galadrix
    New Member
    • Jun 2006
    • 1

    include() in if...else statement?

    When a user logs in my website, it references a character file. In that character file, there is a variable defined, $guild; which if true, has to include a file with guild information.

    So, in the profile page where a user views his/her own information, I need to include the guild file, but since there are more than one guild, I have to include a certain guild file.

    I have this so far.

    Code:
    <?php include('../character_library/'. $_SESSION['name'] .'.inc'); ?>
      <?php
    
    	if ($guild == false) {
    	
    	} else {
    	
    	include('../character_library/guild_library/'$guild'.inc');
    	}
    ?>
    The first part includes the character file, which is in a session when they log in, and the second part needs to include a guild file if they belong to one.

    So, in this case, the character does belong to a guild, let's say, the alpha guild.

    In the character file it says

    Code:
    $guild = "alpha";
    And I have a file on my server called alpha.inc.

    When I insert the statement, and later on in the page I try to access $guildmaster which is in the alpha.inc file, it just hangs, and does NOTHING.

    Thanks for any help you can give me, I'm a bit of a begginner. :-D

    -galadrix
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    [php]
    include('../character_libra ry/guild_library/'$guild'.inc');
    [/php]

    I think that you have a small error in this line, I think you need to use the concatinate string operator . , or at least I would so that it looks like

    [php]
    include('../character_libra ry/guild_library/' . $guild . '.inc');
    [/php]

    Additionally if the page requires the page I suggest that instead of include you use require. The are 4 functions that includes other files into the current script

    include
    include_once
    require
    require_once

    The differences are the require functions will produce an error and abort the script if they fail (making it rather obvious). The _once functions (as opposed to the other ones) only include the given file if it has not already been included (useful for files that start sessions and such like).

    Comment

    Working...