Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Including Php file sets the variables wrong

    Hello I think this is an easy one
    I have written a script which gets the current path, does some evil things to it and comes up with a nice YOU ARE HERE! bar.
    The only problem is if I make a function and include it in say drawnav.php, it prints out the path to the file with the actual function in it, as opposed to the function call file.
    My question is simple... (i hope!) how do i make it throw the file name of drawnav.php instead of nav.php
    Here is the source of nav.php
    PHP Code:
    <?php

    /* Noisyscanner's "You are here" script */

    function drawnav()

    {

    $path dirname(__FILE__); //Get the path of the current script

    $file basename(__FILE__); //Get the current file name

    $dir basename(dirname(__FILE__)); //Get the directory of the current script

    echo '<link href="http://universalgames.biz/nav/navstyles.css" rel="stylesheet" type="text/css">';

    $path explode("/home/brad291/public_html/"$path); // Get rid of the stuff at the beginning, we only want the path after that

    $DirArray explode("/"$path[1]);

    $path[1] = "http://universalgames.biz/$path[1]"// Change the value of the $path[1] variable in the array $path to "Mysite.com/the/path"

    $expfile explode("."$file); // Get rid of the file extension

    $expfile[0] = ucwords($expfile[0]);

    if(
    $expfile[0] == "Index"){ //If the exploded file name (without php html shtml and so forth) is index, set the $dispfile variable to false

    $dispfile false;

    }else{
    $dispfile true;}



    echo 
    '<div class="nav">'// Start the div with the navigation style

    echo "You are here: <a href=\"http://universalgames.biz\">Home</a> > "// Print "You are here" and a link to the homepage.

    foreach($DirArray as $key => $value)// For each $DirArray element, $key is the key and $value is the value

    {

    $ucvalue ucwords($value); // Upper-case the first letter of the word to make it look nice..

    $predir .= "/$value"// Add the current directory being echoed on to the one before...

    echo "<a href=\"http://universalgames.biz$predir\">$ucvalue</a> > "// Echo the links!

    }

    if(
    $file == "play.php")

    {

    $id=$_GET[id];

    mysql_connect("localhost""user""pass") or die(mysql_error());

    mysql_select_db("db") or die(mysql_error());

    $select_game mysql_query("SELECT * FROM games WHERE id='$id'");

    while(
    $row mysql_fetch_array$select_game )) {

    if(
    $dispfile == true)

    {

    echo 
    "<a href=\"$path[1]/$file?id=$_GET[id]\">Playing game: $row[game]</a> "// Echo the file name without the extension. But not if the file is index.

    }

    }

    }else{

    if(
    $dispfile == true)

    {

    echo 
    "<a href=\"$path[1]/$file\">$expfile[0]</a> "// Echo the file name without the extension. But not if the file is index.

    }

    }

    echo 
    "</div>";

    }

    ?>
    and here is the source of drawnav.php
    PHP Code:
    <?php

    include 'nav/nav2/nav3/nav.php';

    drawnav();

    ?>
    Last edited by Fou-Lu; 02-16-2010 at 06:29 AM. Reason: Removed connection info.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Those two scripts sure look the same to me.

    Why are you using a "function"?
    A function is meant to take-in some variables and "return" a value or boolean (true/false).

    Check those two scripts ... why do they look the same?

    And tell us what (or why) it is a function ... or the purpose for the function.

  • #3
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    OOPS!!
    SOrry i posted the same script twice.. look at the edited post now!

  • #4
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It's a function so I can call it in my other script.
    It is supposed to carry out the actions inside the function and return the path of the CURRENT FILE! Not the included file

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by noisyscanner View Post
    It's a function so I can call it in my other script.
    It is supposed to carry out the actions inside the function and return the path of the CURRENT FILE! Not the included file
    Code:
    <?php
    
    include 'nav/nav2/nav3/nav.php';
    
    drawnav(__FILE__);
    
    ?>
    Code:
    function drawnav($incfile)
    {
        $path = dirname($incfile); //Get the path of the current script
        $file = basename($incfile); //Get the current file name
        $dir = basename(dirname($incfile)); //Get the directory of the current script

  • #6
    New Coder
    Join Date
    May 2009
    Location
    Glastonbury, UK
    Posts
    53
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks! This really helped!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •