Major_Disaster Posted March 20, 2008 Share Posted March 20, 2008 Hey guys, Im in the middle of making a website and trying to learn php as i go. Ill try to explain as clearly as i can, but for those who want to cut to the chase, how do i do a php include from a subfolder, calling a file from the root directory? The long winded version: Ive got a site in the following structure: index.php header.php footer.php products/page1.php products/page2.php products/page3.php style.css My header.php includes the following: <head> <link rel="icon" href="favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="style.css" type="text/css" media="all"> <title><?php echo $title; ?></title> <meta name="title" content="<?php echo $metatitle; ?>"> <meta name="description" content="<?php echo $description; ?>"> <meta name="keywords" content="<?php echo $keywords; ?>"> </head> So, in my index.php i have: <?php $title ="Insert title here"; $metatitle ="Insert title here"; $description ="Insert description here"; $keywords ="example, key, word, here"; include ('header.php'); Thats great and works well. But when i tru to do the same from products/page1.php, i get a whole load of problems because the include is looking in the wrong place. I can kinda get it working by using include('../header.php') - but that doesnt apply my css, as that loads the header.php allright, but then just looks for the css in products/style.css! So, i kinda understand what is going on, but i dont know how to fix it. Ive been searching loads, and ive seen something like DOCUMENT_ROOT, which i think could work, but i dont know the actual code to use it. Im thinking, something like <head> <link rel="icon" href="[DOCUMENT_ROOT]/favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="[DOCUMENT_ROOT]/style.css" type="text/css" media="all"> <title><?php echo $title; ?></title> <meta name="title" content="<?php echo $metatitle; ?>"> <meta name="description" content="<?php echo $description; ?>"> <meta name="keywords" content="<?php echo $keywords; ?>"> </head> But when i tried that on my XAMPP test server, firefox read it as C:\XAMPP\style.css - and was unable to load it?! So, what on earth can i do? Cheers for any help. Seriously, im really getting annoyed by this and any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/ Share on other sites More sharing options...
jkewlo Posted March 20, 2008 Share Posted March 20, 2008 why dont u hardcode them in there? instead of using php to include it. Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/#findComment-496952 Share on other sites More sharing options...
Major_Disaster Posted March 20, 2008 Author Share Posted March 20, 2008 Thanks for the response. Really appreciated. Well, the even longer winded version is that my nav bar is pretty big (30 ish links, across 30 ish pages), so id like to have on place to manage them from. Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/#findComment-496957 Share on other sites More sharing options...
rhodesa Posted March 20, 2008 Share Posted March 20, 2008 I usually get around this by using absolute paths to the stuff in a header/footer file. If the site is at the root a URL, like so: http://www.hostname.com/index.php http://www.hostname.com/products/page1.php etc.. then it's easy, just change href="style.css" to href="/style.css". But, if this website "bundle" can be in any subdir like http://www.hostname.com/subdir/index.php http://www.hostname.com/subdir/products/page1.php etc.. I usually just create a variable at the top of header.php called $prefix, set it to the subdir, and then use it to make absolute paths: <?php $prefix = "/subdir/"; ?> <head> <link rel="icon" href="<?php print $prefix;?>favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="<?php print $prefix;?>style.css" type="text/css" media="all"> <title><?php echo $title; ?></title> <meta name="title" content="<?php echo $metatitle; ?>"> <meta name="description" content="<?php echo $description; ?>"> <meta name="keywords" content="<?php echo $keywords; ?>"> </head> Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/#findComment-496964 Share on other sites More sharing options...
maexus Posted March 20, 2008 Share Posted March 20, 2008 I have a master include file that I think will really help out. When I get home, I will post it. Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/#findComment-496967 Share on other sites More sharing options...
rhasan_82 Posted March 20, 2008 Share Posted March 20, 2008 You can modify your header.php by replacing <link rel="stylesheet" href="style.css" type="text/css" media="all"> with <link rel="stylesheet" href="<?php echo $css_path ?>" type="text/css" media="all"> and then in index.php use an extra line $css_path = "style.css"; and in products/page1.php you should write $css_path = "../style.css"; include ('../header.php'); Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/#findComment-496970 Share on other sites More sharing options...
Major_Disaster Posted March 20, 2008 Author Share Posted March 20, 2008 Thanks for the reply rhasan_82 - really helpful. It works! (kinda) The css loads with /style.css, but... My nav bar (the links of which are stored in header.php, and the design in style.css) contains some images. These images load find from index.php, and the site is perfect. But from products/page1.php the images dont load. Im sure this is another case of dogy file links, but what should i have to make it work from all pages? Thanks again for the help. Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/#findComment-496975 Share on other sites More sharing options...
Major_Disaster Posted March 20, 2008 Author Share Posted March 20, 2008 I think i might have it (i think i was being really stupid), give me a sec and ill see. Thanks again for the replies btw. Link to comment https://forums.phpfreaks.com/topic/97120-php-include-with-subfolders-really-appreciate-some-help/#findComment-496977 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.