...

View Full Version : Including Php file sets the variables wrong



noisyscanner
02-15-2010, 08:12 PM
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

/* 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

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

drawnav();

?>

mlseim
02-15-2010, 08:23 PM
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.

noisyscanner
02-15-2010, 08:39 PM
OOPS!!
SOrry i posted the same script twice.. look at the edited post now!

noisyscanner
02-17-2010, 08:12 AM
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

MattF
02-17-2010, 09:10 AM
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



<?php

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

drawnav(__FILE__);

?>




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

noisyscanner
02-17-2010, 09:17 PM
Thanks! This really helped!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum