I'll repeat my earlier sentiments: trying to learn PHP makes me want to rip out my hair lol.

Eventually I'm going to have a couple hundred pages or so loading through this function, so of the three you mentioned, what do you think is the best solution for my needs, and could you detail it a bit more (if possible)? The first one seemed the easiest, but all three of those options kinda flew over my head....

    they're all easy. I would stay away from #3 just becuase if you start doing session stuff, then you don't want to echo directly, you might want to put it in a variable and echo it later, thus minimizing errors.

    THe easiest would be to just use the addenda with the variables. Just open each of your include files and wherever you have $page_Header = or $page_Text = replace it with $page_Header .= and $page_Text .=

    Then none of your other code has to change. You just have to remember that whatever order you include the files in is what order the data will be displayed in (unless you've got CSS which is positioning things absolutely which is a whole other ballgame).

    At this point, the best thing to do is just add that period to the = sign in your include files. If you still don't understand, have a look here at the string operations manual page.

      Not as easy as it sounds 😉

      Added the period, but still haven't made any progress. I'm going to give you all the code that is being ran for one particular page, and perhaps you'll be able to tell me what's wrong. Sorry this is taking so long dude.

      Here's the index page with the function that you made (with 2 modifications by me), and the code that's actually doing the includes/echo:

      <?PHP
      
      function loadInclude($file, $default, $dir='content/')
      {
          if(substr($dir, -1) != '/') $dir .= '/';
      
      if(empty($file) || !$file) $file = $default;
      if(!file_exists($dir . $file . '.php')) $file = $default;
      
      echo 'Including: ' . $dir . $file . '.php<br />';
      
      include($dir . $file . '.php');
      }
      
      loadInclude($_GET['pa'], 'news/news', 'content/'); //I added this bit
      loadInclude($_GET['b'], 'index', 'content/products/books/');
      loadInclude($_GET['cl'], 'index', 'content/products/clothing/');
      loadInclude($_GET['co'], 'index', 'content/products/collectibles/');
      loadInclude($_GET['g'], 'index', 'content/products/games/');
      loadInclude($_GET['mi'], 'index', 'content/products/misc/');
      loadInclude($_GET['m'], 'index', 'content/products/movies/');
      loadInclude($_GET['p'], 'index', 'content/products/posters/');
      loadInclude($_GET['t'], 'index', 'content/products/toys/'); 
      
      $default = include ('content/news/news.php'); //I added this bit
      
      //This is where all the includes/echos are happening
      include('layout/before.php');
      echo $page_Header;
      include('layout/space.php');
      echo $page_Text;
      include('layout/after.php');
      
      ?>

      Here's a short example page: (index.php?pa=chat)

      <?PHP
      $page_Header .= 'Online Chat';
      $page_Text .= '
      
      <div class="main-content-font">
      Sort out IRC after site is uploaded.
      </div>
      
      ';
      ?>

      When I view that page, I've checked my error log and I get no errors, and I get this at the top:

      Including: content/chat.php
      Including: content/products/books/index.php
      Including: content/products/clothing/index.php
      Including: content/products/collectibles/index.php
      Including: content/products/games/index.php
      Including: content/products/misc/index.php
      Including: content/products/movies/index.php
      Including: content/products/posters/index.php
      Including: content/products/toys/index.php

      So it looks like it is calling in the right file, but for some reason none of the echos work. Every single page just echos the $default news/news text.

        It could be that the function is including the files inside that scope. So in your index.php page, add this at the top:

        <?PHP
        
        $page_Header='';
        $page_Text='';

        Then as the first line inside the function add this:

            global $page_Header, $page_Text;

        So your index should start like:

        <?PHP
        
        $page_Header = $page_Text = ''; // Set these to empty strings first
        
        function loadInclude($file, $default, $dir='content/')
        {
            global $page_Header, $page_Text; // pull them in from the main script's scope
        
        if(substr($dir, -1) != '/') $dir .= '/';
        
        if(empty($file) || !$file) $file = $default;
        if(!file_exists($dir . $file . '.php')) $file = $default;
        
        echo 'Including: ' . $dir . $file . '.php<br />';
        
        include($dir . $file . '.php');
        } 
        
        // Continue on....

          Hmm, well it is echoing the right page's variables now... it's just also echoing all the other ones as well (books/index.php, clothing/index.php, etc.). So there's 9 headers and 9 pieces of content being echoed in - and no matter which link I click, they're displayed on the page in the order that the function is calling them:

          Including: content/news/news.php
          Including: content/products/books/index.php
          Including: content/products/clothing/index.php
          Including: content/products/collectibles/index.php
          Including: content/products/games/index.php
          Including: content/products/misc/index.php
          Including: content/products/movies/index.php
          Including: content/products/posters/index.php
          Including: content/products/toys/index.php

          So if I click the Toys link in my nav, it still appears last on the page.

          Is it possible to make the function only include the file whose being called for in the url? For example, if b=anything, only echo files from the content/products/books directory?

          Want me to send you a link to the site so you can have an idea of what we're talking about each time I make changes?

            Yes. A simple if() statement around each loadInclude() would suffice:

            if(isset($_GET['b']))
                loadInclude($_GET['b'], 'index', 'content/products/books');
            
            if(isset($_GET['c']))
                loadInclude($_GET['c'], 'index', 'content/products/collectibles');
            
            // etc., etc., etc.

              Wow, nearly there!

              All the page variables load perfectly, except the default page has stopped working. I tried to add an else statement to each loadIncludes, so if there was no variable it would load the default, but that mustn't be right, or I wrote it wrong, cause the page went nuts.

              This is what I tried:

              else(isset($_GET['pa']))
                    Include $default;

              Currently, $default is:

              $default = 'content/news/news.php';

              Should I have tried echoing $default's contents, rather than including them?

                Else doesn't take any "parameters". It's just an else{}.

                Now, I'm not sure what you're going after here. You want to have a default page for each if() statement? Or do you want a default page if $GET['pa'] isn't set otherwise use $GET['pa']?

                If that's the case, then just remove the if() statement that limits the $GET['pa'] var. Just call "loadInclude($GET['pa'], 'news', 'content/news/');" and it will always load that page, unless $_GET['pa'] is specified as being different that ?pa=news

                  Sorry, I meant I just want one default page.

                  So when no variable is entered, just the domain http://blahblah.com, it loads the content/news/news.php.

                    Well, that's kinda difficult. You can either start using if() elseif() elseif() ... statements and at the very end, use the else{} part to load the news/news.php file.

                    Otherwise, if you want to load any amount of files (as long as they're in the $_GET array) you're kind of limited to this. The only other thing could be to modify the include function to add a couple params. Something like:

                    function loadInclude($key, $array, $default, $dir='content/', $required=false)
                    {
                        global $page_Header, $page_Text;
                    
                    if(substr($dir, -1) != '/') $dir .= '/';
                    
                    $set = isset($array[$key]);
                    
                    if(!$set && $required) $file = $default; // If the array key isn't set, but is required, use default
                    elseif(!$set && !$required) return; // If it's not set and not required, just do nothing
                    else $file = $array[$key]; // Otherwise, it's set let's get the value
                    
                    // If the filename is empty, but required, use the default
                    if((empty($file) || !$file) && $required) $file = $default;
                    else return;  // Otherwise, do nothing
                    
                    // If the file doesn't exist, but is required, use the default
                    if(!file_exists($dir . $file . '.php') && $required) $file = $default;
                    else return; // Otherwise, do nothing
                    
                    // Uncomment the following line to help debug
                    #echo 'Including: ' . $dir . $file . '.php<br />';
                    
                    // Include the file
                    include($dir . $file . '.php');
                    }

                    Then to include files, you just do:

                    loadInclude('pa', $_GET, 'news', 'content/news/', true);
                    loadInclude('b', $_GET, 'index', 'content/products/books/', false);
                    loadInclude('cl', $_GET, 'index', 'content/products/clothing/', false);
                    loadInclude('c', $_GET, 'index', 'content/products/collectibles/', false);
                    loadInclude('g', $_GET, 'index', 'content/products/games/', false);
                    loadInclude('mi', $_GET, 'index', 'content/products/misc/', false);
                    loadInclude('m', $_GET, 'index', 'content/products/movies/', false);
                    loadInclude('p', $_GET, 'index', 'content/products/posters/', false);
                    loadInclude('t', $_GET, 'index', 'content/products/toys/', false);

                    Now, if the specific $_GET key isn't set, and the last parameter is false, it won't include it; however, if it's set to true, then the default will be included.

                    Hope it helps 😉

                      Really? All that?? Wow, I didn't realise default pages were so hard to make, since practically every php-driven site on the net does it lol. They all must have someone as handy as you doing the coding 😉

                      I'm pretty lost though. What do the new loadIncludes do to our old function we had made?

                      We had this:

                      if(isset($_GET['pa']))
                      loadInclude($_GET['pa'], 'news/news', 'content/');

                      And you just suggested this:

                      loadInclude('pa', $_GET, 'news', 'content/news/', true);

                      Do I discard all older stuff now in favour of the code you just wrote?

                      Isn't there an easier way with if/elseif/else statements? Like, "if there's no variable, load $default" or "load $default on start, unless there is a variable" except in PHP coding-language? lol.

                        You can yes. Since the new function will check to see if whatever key you send (here "pa") is inside whatever array you send with it (here $_GET).

                        No, not all sites are this difficult to code a default page for. It just so happens that your layout or site structure is designating how you write your code. This usually ends up with some "bad" code.

                        The usual way I handle this is to route all requests through a central script and if things fail through either a switch statement or if()elseif() case then a default page is done. But my stuff is usually database driven so it's as simple as checking if I have a real result or not, not if the pages are actually there.

                          As you can tell I'm a super newb, so I chose doing pages for now because I was finding the database stuff pretty difficult and I wanted to start off with something simpler and maybe go to a database-driven site later (Since what Im doing would work very well database-driven). Evidently though, the PHP stuff isn't that easy either...

                          Ok so I'm gunna give this a shot and try to learn something, before I go ahead and add yours in to my code... Using if/elseif/else like you mentioned, would something along these lines work?

                          if(isset($_GET['pa']))
                          loadInclude($_GET['pa'], 'news/news', 'content/');
                          
                          elseif(isset($_GET['b']))
                          loadInclude($_GET['b'], 'index', 'content/products/books/');
                          
                          elseif(isset($_GET['cl']))
                          loadInclude($_GET['cl'], 'index', 'content/products/clothing/');
                          
                          elseif(isset($_GET['co']))
                          loadInclude($_GET['co'], 'index', 'content/products/collectibles/');
                          
                          elseif(isset($_GET['g']))
                          loadInclude($_GET['g'], 'index', 'content/products/games/');
                          
                          elseif(isset($_GET['mi']))
                          loadInclude($_GET['mi'], 'index', 'content/products/misc/');
                          
                          elseif(isset($_GET['m']))
                          loadInclude($_GET['m'], 'index', 'content/products/movies/');
                          
                          elseif(isset($_GET['p']))
                          loadInclude($_GET['p'], 'index', 'content/products/posters/');
                          
                          elseif(isset($_GET['t']))
                          loadInclude($_GET['t'], 'index', 'content/products/toys/');
                          
                          else include $default; 

                          With the default set as:

                          $default = 'content/news/news.php'

                            Assuming that you only want to include one file per-request, regardless of whether or not there are multiple things set..

                            E.g. a URI like: index.php?pa=news&b=school&m=
                            OR
                            a URI like: index.php?b=school&t=toddler&pa=news

                            would default to strictly the "pa" page because it's set, and in your code it's the first in your succession of checks. THe only way around that is to use something similar to the function I gave you and define whether it's "required" or not.

                            I hope that makes sense to you.

                              It kind of makes sense... I've still got a lot of learning to do.

                              Your function is still slightly bugged, I ended up trying it and nothing was getting included. Even the debug at the top didn't show up.

                              But, don't worry, I'll leave you alone! For now, I'm only going to be including a single file per request, and I tried the code that I just posted above and as far as I can tell it is working.

                              So many, many thanks to you, for putting the function together, and helping me understand PHP a little better. Even though you'll probably never see it, I've added your name to my credits page. If I ever need help again, don't be surprised if you see another thread by me. Hopefully not too soon though...

                              I'd buy you a case of beer if I could.

                              Thanks again dude.

                                No problem. Don't forget to mark this as resolved if it is.

                                  Write a Reply...