Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2010, 08:12 PM   PM User | #1
noisyscanner
New Coder

 
Join Date: May 2009
Location: Glastonbury, UK
Posts: 53
Thanks: 1
Thanked 0 Times in 0 Posts
noisyscanner is an unknown quantity at this point
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 05:29 AM.. Reason: Removed connection info.
noisyscanner is offline   Reply With Quote
Old 02-15-2010, 08:23 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,044
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 02-15-2010, 08:39 PM   PM User | #3
noisyscanner
New Coder

 
Join Date: May 2009
Location: Glastonbury, UK
Posts: 53
Thanks: 1
Thanked 0 Times in 0 Posts
noisyscanner is an unknown quantity at this point
OOPS!!
SOrry i posted the same script twice.. look at the edited post now!
noisyscanner is offline   Reply With Quote
Old 02-17-2010, 08:12 AM   PM User | #4
noisyscanner
New Coder

 
Join Date: May 2009
Location: Glastonbury, UK
Posts: 53
Thanks: 1
Thanked 0 Times in 0 Posts
noisyscanner is an unknown quantity at this point
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
noisyscanner is offline   Reply With Quote
Old 02-17-2010, 09:10 AM   PM User | #5
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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
MattF is offline   Reply With Quote
Old 02-17-2010, 09:17 PM   PM User | #6
noisyscanner
New Coder

 
Join Date: May 2009
Location: Glastonbury, UK
Posts: 53
Thanks: 1
Thanked 0 Times in 0 Posts
noisyscanner is an unknown quantity at this point
Thanks! This really helped!
noisyscanner is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:01 AM.


Advertisement
Log in to turn off these ads.