View Full Version : Navigation Variable
I want to throw my navigation content into one PHP variable so that I can just make any changes to it in one or two steps instead of having to redo each page. I am very unsure of how to do this from a structure standpoint. I have a few options, please advise me on the best course of action:
1) Make all navigation liks go to "http://www.mydomainname.com/file.htm" instead of just "file.htm"
2) Make a new php variables file for each level of my web so parent level can contain link "file.htm" while the next level can contain "../file.htm"
3) OMGLAZORSPEWPEWPEW
I can't figure out which would be the best way to go...and maybe you have an idea that will better suit what I need. Please help me out on this hierarchial syntax problem. :thumbsup:
Velox Letum
11-15-2005, 12:15 AM
1) Just make your links /file.html and the browser translates it to http://domain.dom/file.html.
2) You mean a current directory? You could get that using pathinfo() (http://www.php.net/pathinfo) and $_SERVER['REQUEST_URI'];. If you really want ../ then replace the directory names with .. so instead of /subdir/subdir2/file.html it'll be ../../file.html.
3) See brick.
Ok lets see if I can refine this question...
I do have 1 file with all my php variables, and the $navigation is one of them. In that navigation I need to link to vairous pages. I understand that /file.php will link to http://domain.com/file.php, but if I have just 1 file linked to by all sublevel pages, then file.php will be different or non-existent for each subfolder in the web. (http://domain.com/floorplans/file.php will be linked to instead of the desired http://domain.com/file.php for files in the floorplans subfolder). If I do ../file.php, the parent directory files will be misled to a level above themselves. To solve this I have 2 options:
1)Type out every link as "http://www.domain.com/w00t.php"...this solves the multiple level problem by pointing out a specific page to reach, but kills my client side testing of scripts and fixes pre-publishing.
OR
2)Keep the links as "../lolersk8ts.php" and have a new php nav variable for each level of the directory as to direct "lolersk8ts.php" on parent level, "../lolersk8ts.php" for a folder, and "../../lolersk8ts.php" for a folder in a folder.
I am asking which one should I do? If you have a better solution, please show me what you have.
firepages
11-15-2005, 01:22 AM
I do have 1 file with all my php variables
& is that file included in each page ? if so then simply set or define a variable in that file..
<?//config.php
DEFINE('ROOT','http://localhost/blah');
DEFINE('FILE_ROOT','c:/blah/blah/');
//rest of your config...
mysql_connect(...etc
?>
then in your pages <?=ROOT;?>/blah/whatever.php for links and images && <?=FILE_ROOT;?>/blah.inc for includes
This way you only need to change one file to change the root (for when testing (or even use a conditional to set the root)).
Regardless I find absolute paths to be easier to maintain in the long run , if you do start using PATH_INFO etc then relative paths get screwed up quite quickly.
Note that technically, DEFINE'ing strings is not ideal , constants are normally defined as integers, but the above works for me so I live with it :D
& is that file included in each page ? if so then simply set or define a variable in that file..
<?//config.php
DEFINE('ROOT','http://localhost/blah');
DEFINE('FILE_ROOT','c:/blah/blah/');
//rest of your config...
mysql_connect(...etc
?>
then in your pages <?=ROOT;?>/blah/whatever.php for links and images && <?=FILE_ROOT;?>/blah.inc for includes
This way you only need to change one file to change the root (for when testing (or even use a conditional to set the root)).
Regardless I find absolute paths to be easier to maintain in the long run , if you do start using PATH_INFO etc then relative paths get screwed up quite quickly.
Note that technically, DEFINE'ing strings is not ideal , constants are normally defined as integers, but the above works for me so I live with it :D
:eek: Wow, I have no idea what you just said.
Well ok I have some idea. (BTW: im just starting to learn php & mysql...havent even started on mysql yet but I will need to know it). So I understand that DEFINE('string name','mixed value'); defines a variable "string name" as "mixed value". But now I have a few questions
How is this different than $string_name = 'mixed value';???
I dont understand what the ROOT and FILE_ROOT do. I have some ideas but I don't want to type them out and confuse you and myself. Do you have a good link that explains? Thanks for the headstart tho! Looking forward to hearing back soon.
marek_mar
11-16-2005, 11:10 PM
define() defines a constant, not a variable.
firepages
11-17-2005, 01:15 AM
OK so lets create variables instead of constants
<?//config.php
$ROOT = 'http://localhost/blah';
$FILE_ROOT = 'c:/blah/blah/';
//rest of your config...
mysql_connect(...etc
?>
the plan is that you include that file in all of your pages that require it, most sites have at least 1 config file that is included on every page to provide database connections , variable & constant definitions etc..
then instead of writing relative paths in your pages you use the $ROOT or $FILE_ROOT
.................blah.php..................
<?php
include_once '../config.php';
include_once $FILE_ROOT.'/include/stuff.php';
?>
<html>
....etc
<body>
<a href="<?=$ROOT;?>/dir/nextpage.php;?>">a link</a>
</body>
</html>
...you still have the relative path call to config.php which will differ from page to page , but after that all your other paths are set using $ROOT && $FILE_ROOT so you dont have to mess with them again , just change $ROOT && $FILE_ROOT instead.
if you instead DEFINE('ROOT',etc
then you simply <?echo ROOT;?> instead of <?echo $ROOT;?>
Oooh man I am so close to getting this now, but it still won't work! I know why but I don't know how to fix it! With the way you started me out on, I get a 404 error for http://localhost/WEB//index.php. Oh by the way I found your conditionals at this link (http://www.firepages.com.au/FUD/index.php?t=msg&goto=2836&rid=0&S=3b8d81dad69e98997383d8c0cc193d54#msg_2836) The double slash is where the problem is, and heres how it occured:
My navigation is in the construct.php as a variable, so I have
$navigation = <<<PARA
<ul id="nav">
<li><a href="index.php">Home</a></li>...etc.
While this is in the very first part of my construct.php (with the correct paths of course):
<?php
if($_SERVER['REMOTE_ADDR']=='127.0.0.1'){
DEFINE('ROOT' , 'http://localhost/username' );
DEFINE('FILE_ROOT' , 'e:/phpdev/www/username' );
}else{ //online//
DEFINE('ROOT' , 'http://username.com' );
DEFINE('FILE_ROOT' , '/home/user/www/username' );
}
?>
but in order to do a link while I am still in php and using the root, I have tried to do <a href="<?=ROOT;?>index.php">, but that starts PHP up again when I am already in it. So the I decided to go with
<a href="ROOT/index.php">
but that ended up in a result of // for the link and produced a 404! I cannot very well do <a href="ROOTindex.php"> as there needs to be a seperator between ROOT & index.php. WTF do I do here to make these links go through when I am already in PHP!
Ok so I think I got the links to work, but not correctly 100%. I got the 404 to go away by using variables instead of constants, but I still end up getting the pathway http://localhost/web//file.php. It displays fine, but I really wish I could get rid of that extra / someway.:confused:
EDIT: Oh wait I got it now...I had an extra / in there after I set them to $ROOT and $FILE_ROOT. Still have 1 question tho...what does $FILE_ROOT do?
firepages
11-22-2005, 12:30 AM
......what does $FILE_ROOT do?
Often your php pages will need to include() or require() other PHP pages , to avoid getting pre-parsed pages you need to specify a file-path to a file rather than a url , again I prefer absolute file paths to relative ones so its the same principle.
include_once FILE_ROOT."/include/templates/{$blah}.tpl";
Ok so just to check if I have this right: use $ROOT for hyperlinks and the like, but use $FILE_ROOT for any includes or requirements such as .css, .js, and any extra .php files other than construct.php?:confused:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.