About a year ago deathshadow was trying to help me learn how to code a simple responsive website. He actually did an entire site for me which I greatly appreciate. I have not done much with continuing my education due to some medical situations that have developed over the past year, but now I am ready to get it on with it. Anyway, this involves the work that deathshadow did on my site amazingbarbeque dot com. In looking over the code for any of the pages, the links in the sidebar under Popular Articles and How To Articles are hard coded into each individual page. Also included in the pages that Jason prepared for me is a sub-directory called extras that has a file called popularArticles.extras.php as well as howToArticles.extras.php which contain basically the same code. My first question is, is it possible to use something like a php include statement to call up either popularArticles.extras.php or the howToArticles.extras.php file so that in the event that changes are ever made to these lists, all I have to do is change the appropriate file instead of having to go through each individual page?
Yes, of course you can do that. You need to create some "shared" file (let's call it "settings.php") and include all the files (you want to be automatically included) inside it. Then if you include settings.php in any of your pages, settings.php will also include all the depending files (listed in settings.php itself) too. I'm not very good at explaining basic stuff, but hope you got the idea.
Thanks phpmillion. I wasn't sure about this so I thought it would be a good idea to check with the experts. What I did was modify the code to read: <div id="popularArticles"><h2>Popular Articles</h2> <?php include("popularArticles.php"); ?> <!-- #popularArticles.subsection --></div> I created a file called popularArticles.php and loaded it to the root directory. However, when I call up the page with the 'include' statement I get this: Warning: include(popularArticles.php): failed to open stream: No such file or directory in /web/sites/pimpdaddy/test.thebams.net/guides/photograpy_tips.phpon line 160 If I load the popularArticle.php file in my 'guides' subdirectory and it works fine. While I am a real dummy when it comes to php, I was under the impression that the above 'include' statement should be called up from the root directory. Am I wrong in my thinking or are the elements of this 'include' statement incorrect? Thanks again . . .
It expects the file to be in the url relative to your PWD (present working directory). That's why you found it in "guides". To go absolutely to the root directory, preface the filename with '/' as in <?php include("/popularArticles.php"); ?> Code (markup): gary
Thanks Gary . . . I tried doing as you suggested, but for some reason it does not work for me. It gives me the error message that it is still trying to find the file in the sub-directory 'guides' so I think I will just upload the file to the different sub-directories.
if you have the included file in a directory above the PWD then you need to reference the path right down from the root. one way to insure that you always find the file you are looking for is to set a path variable: $rootPath='root/diectory/sub_dir'; then it will beable to find with include $rootPath.'filename'. you can go down as many sub-directory levels as you want as long as all of then are in either the root variable, or are part of the target file name.
Yes, this is exactly what I needed. If I understand right, both photography_tips.php and popularArticles.php files are located inside /guides directory? And you added this code: <?php include("popularArticles.php"); ?> PHP: into file photography_tips.php, right? If so, you did everything perfectly and no error message should be displayed at all. Maybe basic browser cache cleanup and forced page reload will fix i?
Thanks again phpmillion. I really appreciate your input on this subject. I did some playing around with the code and boy do I feel extra stupid. All I needed to do was add "../popularArticles.php" and it is now working like a charm.
Just one little thing -- if you have to link "up-tree" using ../, there is possibly something wrong with your directory structure / file organization -- but that hinges on if you are using the "one index to rule them all" approach to PHP or not. Even so, I try to limit all user-callable files to the root (part of why I favor 'one index') so that all includes point downwards, not upwards. This practice is also why template images end up in template/images (assuming the CSS is in /template/) and content images end up in /images. Also makes it easier to keep things portable and aid in re-skinning. I just try to avoid ../ as much as possible. Part of why I filter out / disallow runs of multiple periods in my URI splitting. Side note, good to hear things worked out well for you. I toss a lot of these one-off's out there so it's always nice to hear back about them. 'Twas a simple page but sometimes simple is best. Content is always more important than the do-dads. That said these days i'd suggest getting some social media links on there next.