SalientAnimal Posted May 3, 2014 Share Posted May 3, 2014 Hi all, I don't understand why my includes file ins't working. When I add the files inside my includes file to the page it all works as expected. I then try moving the CSS & JS files out of the main file and into and includes file that is located in my includes directory. Then it stops working. Any guidance here? Directory structure is: Main Directory = htdocs (Houses my main file) Includes Directory = includes (Houses css_includes.php & js_includes.php) JS Directory = js (Houses all the mentioned *.js files) CSS Directory = css (Houses all the mentioned *.css files) This is how I am including it in my main file: <?php include_once 'includes/css_includes.php'; include_once 'includes/js_includes.php'; ?> Inside each directory to have: css_includes.php <link type="text/css" rel="css/stylesheet" href="../css/reset.css"> <link type="text/css" rel="css/stylesheet" href="../css/metallic.css"> <link type="text/css" rel="css/stylesheet" href="../css/style.css"> <link type="text/css" rel="css/stylesheet" href="../css/shCoreDefault.css"> js_includes.php <script type="text/javascript" src="../js/XRegExp.js"></script> <script type="text/javascript" src="../js/shCore.js"></script> <script type="text/javascript" src="../js/shLegacy.js"></script> <script type="text/javascript" src="../js/shBrushJScript.js"></script> <script type="text/javascript" src="../js/shBrushXML.js"></script> <script type="text/javascript" src="../js/jquery-1.10.2.js"></script> <script type="text/javascript" src="../js/zebra_datepicker.js"></script> <script type="text/javascript" src="../js/core.js"></script> When adding these to the main file I just remove the " ../ " in front of each of the src / href and it works perfectly. Quote Link to comment Share on other sites More sharing options...
trq Posted May 3, 2014 Share Posted May 3, 2014 So.. what is happening exactly? Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted May 3, 2014 Author Share Posted May 3, 2014 My page is not loading any of the js / css files when trying to have them in the separate includes file. So I just get a regular html page. Quote Link to comment Share on other sites More sharing options...
trq Posted May 3, 2014 Share Posted May 3, 2014 Does... <link type="text/css" rel="css/stylesheet" href="../css/reset.css"> <link type="text/css" rel="css/stylesheet" href="../css/metallic.css"> <link type="text/css" rel="css/stylesheet" href="../css/style.css"> <link type="text/css" rel="css/stylesheet" href="../css/shCoreDefault.css"> Appear in your source ? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2014 Share Posted May 3, 2014 when you use php to include a file, it essentially replaces the include() statement with the content of the file being included. it doesn't matter where the file being included is stored at, it could be twenty folders deep in your site or even below/outside your document root folder, any links in the resulting web page are relative to resulting web page, they aren't relative to where the included file is stored at. Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted May 3, 2014 Author Share Posted May 3, 2014 Does... <link type="text/css" rel="css/stylesheet" href="../css/reset.css"> <link type="text/css" rel="css/stylesheet" href="../css/metallic.css"> <link type="text/css" rel="css/stylesheet" href="../css/style.css"> <link type="text/css" rel="css/stylesheet" href="../css/shCoreDefault.css"> Appear in your source ? No, its in my includes files. When it was in the source less the ../ it worked fine. But when I moved to an includes directory, I had to add the ../ to browse more folders down. Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted May 3, 2014 Author Share Posted May 3, 2014 when you use php to include a file, it essentially replaces the include() statement with the content of the file being included. it doesn't matter where the file being included is stored at, it could be twenty folders deep in your site or even below/outside your document root folder, any links in the resulting web page are relative to resulting web page, they aren't relative to where the included file is stored at. Not sure what you mean here? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2014 Share Posted May 3, 2014 But when I moved to an includes directory, I had to add the ../ to browse more folders down. no you didn't, because it's not the browser that's including the file, it's php. the source that trq referred to is the 'view source' in your browser. the resulting html/css/javascrpt that's output to the browser must be correct. Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted May 3, 2014 Author Share Posted May 3, 2014 Ok, so what am I doing wrong? I'm sorry but you guys are being very vague Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted May 3, 2014 Share Posted May 3, 2014 Tell us what you see when you right click > view source, Check to make sure the HTML output by your PHP code is correct (The PHP code should not be present). Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted May 3, 2014 Author Share Posted May 3, 2014 (edited) This is what I see when I go to view source: <!doctype html> <html> <head> <meta charset="utf-8"> <title></title> <script src='../js/XRegExp.js' type='text/javascript'></script> <script src='../js/shCore.js' type='text/javascript'></script> <script src='../js/shLegacy.js' type='text/javascript'></script> <script src='../js/shBrushJScript.js' type='text/javascript'></script> <script src='../js/shBrushXML.js' type='text/javascript'></script> <script type='text/javascript'> SyntaxHighlighter.defaults['toolbar'] = false; SyntaxHighlighter.all(); </script> <script src='../js/jquery-1.10.2.js' type='text/javascript'></script> <script src='../js/zebra_datepicker.js' type='text/javascript'></script> <script src='../js/core.js' type='text/javascript'></script> <script src='../js/my_location.js' type='text/javascript'></script> <link type='text/css' rel='css/stylesheet' href='css/reset.css'><link type='text/css' rel='css/stylesheet' href='css/metallic.css'><link type='text/css' rel='css/stylesheet' href='css/style.css'><link type='text/css' rel='css/stylesheet' href='css/shCoreDefault.css'><body></body> </head> That's only the top section of the code, up until the head closing tag Edited May 3, 2014 by SalientAnimal Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted May 3, 2014 Share Posted May 3, 2014 Okay so is the file paths correct to your JavaScript and CSS files? Remember the file paths used in the HTML must be relative to your sites (current) url. Where as the file paths used in PHP are relative to where the script is located on the file system. Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted May 3, 2014 Author Share Posted May 3, 2014 (edited) Ok, so the directory structure i have is a follows... Just to see if maybe I am missing something. htdocs is the main directory as it is by default. Inside this directory I have three directories includes js css inside the includes directory I have my two include files (the include files contain the code as show through the "view source code"): css_includes.php js_includes.php for testing my main file (test.php) is in this directory as well, however I want it to eventually be directly in htdocs Inside the js directory I have all the js files that are being shown in the source Inside the css directory I have all the css files that are being shown in the source If I add the file location directly into my file (test.php) without using the includes it works perfectly. However i want to keep them out of the file itself so that in future I can just include all the required scripts with a single include. Hope this makes sense? Edited May 3, 2014 by SalientAnimal Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted May 3, 2014 Share Posted May 3, 2014 (edited) The problem then is most likely the .. at the start of the file path in your HTML. Just start your HTML file paths with a / instead css_includes.php <link type="text/css" rel="css/stylesheet" href="/css/reset.css"> ...etc <link type="text/css" rel="css/stylesheet" href="/css/shCoreDefault.css"> js_includes.php <script type="text/javascript" src="/js/XRegExp.js"></script> ...etc <script type="text/javascript" src="/js/core.js"></script> Edited May 3, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted May 3, 2014 Author Share Posted May 3, 2014 Thanks, I managed to get it to work. I had single quotes instead of double quotes around the sources etc... To be honest I didn't know this made a difference. I also removed the .. in front of each file just for safe keeping and it seems to be working. It just appears as if not all the css and js files are loading, which I struggle to understand. Thanks for the assistance. Quote Link to comment Share on other sites More sharing options...
Solution trq Posted May 4, 2014 Solution Share Posted May 4, 2014 Thanks, I managed to get it to work. I had single quotes instead of double quotes around the sources etc... To be honest I didn't know this made a difference. It doesn't. I also removed the .. in front of each file just for safe keeping and it seems to be working. That indeed would have been the cause of your issue. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.