Jump to content

PHP templating assistance?


DenHepLei

Recommended Posts

Hi,

 

I was following along in the tutorial "Introduction to PHP templating" http://phpro.org/tutorials/Introduction-to-PHP-templating.html and some have questions.

 

Everything works as it should in the example as long as the files to be included are located in the directory root, however I would like to keep my include files in a subdirectory called "includes" and need to know how the example needs to be modified to accomplish this?

 

I posted earlier in this forum with a simular questions but may not have been clear in what I am trying to accomplish, I apologize for the semi-duplicate post.

 

Thanks for the assistance.

Link to comment
Share on other sites

From

 

include_once("main.php");

 

To

 

$folder = "includes/";
include_once($folder."main.php");

 

Just add the folder onto the front of the file name and use the url to get what page to access, etc... I just used main.php as an example since they did use that.

Link to comment
Share on other sites

Hi,

 

Humm.. I must be doing something wrong, it's still not working? I have made some changes to the example, I am using the extension .inc rather than .php for the files containing the content I want displayed and I have also changed the default page from main.php to index.php, here is the code I am tyring to get working, I have a simple menu and what I want to see happen is that when you click a letter such as "A" for example the content from the file includes/a.inc is displayed, when letter "M" is clicked the content from includes/m.inc is displayed and so on...

 

<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=a>A</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=c>C</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=d>D</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=e>E</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=f>F</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=g>G</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=m>M</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=n>N</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=p>P</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=s>S</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=t>T</a>"; echo "  ::  " ?>
<?php echo "<a href=". $_SERVER['PHP_SELF']."?page=y>Y</a>"; ?>

<!-- Begin Dynamic Content -->

<?php
  // create an array of allowed pages
  $allowedPages = array('a', 'c', 'd', 'e', 'f', 'g', 'm', 'n', 'p', 's', 't', 'y');
  
  // check if the page variable is set and check if it is the array of allowed pages
  if(isset($_GET['page']) && in_array($_GET['page'], $allowedPages))
    {
    // first check that the file exists
    if(file_exists($_GET['page'].'.inc'))
        {
        //  If all is well, we include the file
        include_once($_GET['page'].'.inc');
        }
    else
        {
        // A little error message if the file does not exist
        echo 'No such file exists';
        }

    }
  // if somebody typed in an incorrect url
  else
    {
    // if things are not as they should be, we included the default page
    if(file_exists('index.inc'))
        {
        // include the default page 
      $folder = 'includes/';
      include_once($folder.'index.inc');
        }
    else
        {
        // if the default page is missing, we have a problem and it needs to be fixed.
        echo 'index.inc is missing. Please fix me.';
        }
    }

?>

<!-- End Dynamic Content -->

Thanks in advance

Link to comment
Share on other sites

Didn't add the includes folder here (so do it):

 

      $folder = 'includes/';
  // check if the page variable is set and check if it is the array of allowed pages
  if(isset($_GET['page']) && in_array($_GET['page'], $allowedPages))
    {
    // first check that the file exists
    if(file_exists($folder.$_GET['page'].'.inc'))
        {
        //  If all is well, we include the file
        include_once($folder.$_GET['page'].'.inc');
        }
    else
        {
        // A little error message if the file does not exist
        echo 'No such file exists';
        }

    }
  // if somebody typed in an incorrect url
  else
    {
    // if things are not as they should be, we included the default page
    if(file_exists($folder.'index.inc'))
        {
        // include the default page 

      include_once($folder.'index.inc');
        }
    else
        {
        // if the default page is missing, we have a problem and it needs to be fixed.
        echo 'index.inc is missing. Please fix me.';
        }
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.