1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Still Trying To Learn . . . Simple Questions?

Discussion in 'HTML & Website Design' started by VanceVP, Apr 18, 2018.

  1. #1
    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?
     
    VanceVP, Apr 18, 2018 IP
  2. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #2
    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.
     
    phpmillion, Apr 19, 2018 IP
  3. VanceVP

    VanceVP Member

    Messages:
    113
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    33
    #3
    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 . . .
     
    VanceVP, Apr 19, 2018 IP
  4. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #4
    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
     
    kk5st, Apr 19, 2018 IP
  5. VanceVP

    VanceVP Member

    Messages:
    113
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    33
    #5
    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.
     
    VanceVP, Apr 19, 2018 IP
  6. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #6
    Can you make a screenshot with your directory/file structure?
     
    phpmillion, Apr 20, 2018 IP
  7. VanceVP

    VanceVP Member

    Messages:
    113
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    33
    #7
    I hope this is what you are asking for:

    [​IMG]
     
    VanceVP, Apr 20, 2018 IP
  8. COBOLdinosaur

    COBOLdinosaur Active Member

    Messages:
    515
    Likes Received:
    123
    Best Answers:
    11
    Trophy Points:
    95
    #8
    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.
     
    COBOLdinosaur, Apr 20, 2018 IP
  9. VanceVP

    VanceVP Member

    Messages:
    113
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    33
    #9
    Thanks COBOLdinosaur. That makes a lot of sense. I will give it a shot and see what happens.
     
    VanceVP, Apr 20, 2018 IP
  10. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #10
    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?
     
    phpmillion, Apr 21, 2018 IP
  11. VanceVP

    VanceVP Member

    Messages:
    113
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    33
    #11
    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.
     
    VanceVP, Apr 21, 2018 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #12
    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. :D
     
    deathshadow, Apr 21, 2018 IP