a REALLY simple php question

Discussion in 'Web Design and Development (archive)' started by brianellisrules, Nov 5, 2003.

  1. macrumors regular

    Joined:
    Oct 17, 2003
    #1
    I feel dumb for asking this, but whatever.

    Here's my current page for my stories: http://www.brianellisrules.com/stories/

    There are two .php files in the directory. The first one displays a menu of all the stories I have, the second actually displays the stories.

    index.php looks like:
    PHP:
    <?php
    include('/home2/brian/public_html/head.inc');
    include(
    'menu.inc');
    ?>
    menu.inc is just a file with links to all my stories... which are also .inc files

    story.php looks like:
    PHP:
    <?php
    include('/home2/brian/public_html/head.inc');
    include(
    $story.'.inc');
    ?>
    I'm setting the $story variable when I link to each story... (http://www.brianellisrules.com/stories/story.php?story=about)

    I'm sure there's a way to include an if statement, and do all this with one file... I'm just a moron. I'm thinking the basic structure would look like:
    PHP:
    <?php
    if ($storynull);
    {
    include(
    menu.inc);
    }
    else;
    {
    include(
    $story.'.inc');
    }
    ?>
    Also, how are variables stored? Let's say, I get this setup working... if someone goes to the page for the first time, there's no value for $story, so the menu.inc file would be displayed. Perfect. From there, the person would select one of the stories, so $story would now have a value. Perfect. If they wanted to go back to the story menu page and clicked the link for "http://www.brianellisrules.com/stories/" the index.php file would be loaded... but would the value for $story be saved? Would it automatically load the most recent story instead of the menu? If so, how do I get around this?
     
  2. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #2
    you can set an IF on a null variable by checking to see if that variable is defined:

    PHP:
    if (!$var) { then } else { something }
    remember, the ! is a negation in PHP, so you're saying: IF $var DOES NOT exist, THEN execute; ELSE execute something else.

    which is exactly how you're using it in this case: IF no story is selected, THEN display a menu; ELSE display the story that is selected.

    your second question is one of scope. in general, any variable you define has its scope within a single script, including includes/requires:
    PHP:
    $a 1;
    include (
    'foo.inc');
    will make $a available to foo.inc.

    if you don't pass a variable to another script with a GET, POST or some other method, it won't exist in the second script.

    in other words, story.php?story=foo passes the variable $story with value foo via the HTTP GET. but if you just call story.php without passing the story variable in some other way (via a POST by defining it in a form, for instance) then $story will not exist. and if you've included a conditional, if (!$story) will evaluate to true.

    let me know if that makes sense or not.

    for more information: php.net/variables.scope
     
  3. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #3
    Awesome, thanks for the help. I had a feeling the first part could easily be answered like that.

    As for the second part... I needed to go over it a second time, but I understand it. Thanks again.
     
  4. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #4
    sorry. i was afraid i'd gotten a little vague; but i was attempting to explain the reason things work that way, instead of just saying "do this because the manual says so."

    long story short: the scope of a variable is (usually) one and only one script. so if you load a script (in this case, story.php) without defining your variable ($story) in either the URL or in the body of the script, it's going to go undefined.

    therefore: story.php?story=foo defines $story. but even though it's defined, once you reload story.php, you're leaving the scope of the variable and it won't carry over -- you don't need to do anything extra to unset it.
     

Share This Page