Jump to content

Recommended Posts

Be forewarned: noob ahead. ???

 

I need a bare-bones template script for my website, to keep my navigation and stuff consistent. After about 20 pages in HTML, I found I was missing pages whenever I updated something, unless I went back and rechecked all the pages manually. I really don't have the time for that, so I decided to whip up a little template system in PHP. Unfortunately, my PHP skills are about on par with those of your average pile of rocks. :D So far, all I know is that I need to remove the content from a copy of one of my pages and put the <?php and ?> tags where it used to be. After this point, I know I want to have PHP open a second file, $page or something, and send its contents to be processed as HTML, then close and let the rest of the page be sent untouched. Problem is, I don't know how to do any of that. If anyone could give me an example, or even a full working script, I would really appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/84940-solved-template-script/
Share on other sites

First off make sure you read up on css (not inline).  It can be your best friend. 

As far as your navigation, you can start with something simple...

 

say you visit www.site.com/index.php?page=home

 

<?php

switch($_GET['page']){
    case 'home':
        include("home.php");
        break;
    case 'page2':
        include("page2.php");
        break;
    //and so on...
}
?>

Chris: I do plan to learn CSS, but I have a few dozen other things to do first. :-\

As for your code, it seems to me that that might not scale very well. I'm looking for a script that I won't need to change for a long time, and as far as I can tell, that's not the case here. I want a script that starts off with static HTML up to the point where I would add content. From there, PHP kicks in, and takes a filename from the URL and outputs the contents of the file. That file will be my content for that page, formatted in HTML to fit in just like it was the content of my old pages. After displaying that file, PHP is finished, and the rest of the page is transmitted to the user.

 

Let's use your example, www.site.com/index.php?page=home

In this case, index.php is essentially an HTML file with a PHP script to load the contents of home.html and output them where my content would normally be on my old HTML pages. The rest of the script is just the HTML that comes after that content. Do you understand what I'm asking for?

like i said start learning on that link i gave you and i guess you mean this

 

let say your url look like this

teng.php?pages=contact

 

swicth($_GET['pages']){
   case 'contact':
        //display contact here
        include 'con.php';
  break;
  case 'etc.....':
    // stuff here
  break;

}

teng: the link you gave me was for $_POST, a particularly unhelpful solution to my problem. As much as I like submit buttons, there will no doubt come a time when I would like text links on my site.  :)  I did, however, find the link on that page to $_GET, so thank you.

 

As for what I was describing, you both have the wrong idea in mind. I don't want to have to maintain what is essentially a directory of every page on my site, if possible, I'd like to know if PHP could work like so:

<html>
(Stuff)
<?php
echo "contents_of_$page" 
?>
(More stuff)
</html>

Not sure if this is right, but $page is supposed to indicate index.php?page=this_string_here

 

Thanks for the help and quick replies.

im almost there!!!

 

the get variables will get the data that you want to include

in my sample i use include but i guess you need a simple statement that will determine what to echo

 

 

swicth($_GET['pages']){

  case 'contact':

        //display contact here

        echo stuff here

  break;

  case 'etc.....':

    //echo  stuff here

  break;

 

}

 

i didnt say stick on that page but i mean start learning on that page..

hope this second sample i gave you makes sense

 

 

 

So, if I'm understanding this correctly, this script will get 'pages' and look for a match in index.php, and when it finds one, it echoes 'stuff here'.

That's not what I'm looking for. I don't want to keep my content in index.php, I want to keep the content for each separate page in its own file, so I can more easily add pages. Think of it this way: When someone accesses index.php?page=home, the PHP script loads the file 'home' from the directory it's in and outputs it. The script doesn't search for a match listed in index.php and then display the associated info. If I did it that way, index.php would be several MB in size. This way, I can easily add new pages just by uploading one small file, rather than downloading several MB of data, finding what I want to edit, editing it, and uploading it again.

<?php

  if (isset($_GET['page'])) {
    if (file_exists($_GET['page'] . '.php')) {
      include $_GET['page'] . '.php';
    }
  }

?>

 

Passed a url like http://yoursite.com/index.php?page=foo this script would load foo.php. Im still not sure this is what your looking for.

 

This way, I can easily add new pages just by uploading one small file, rather than downloading several MB of data, finding what I want to edit, editing it, and uploading it again.

 

If your going to use php you may as well do it properly and store your content within a database. Make a simple admin area to add new content, and bam! A new page.

Please be very careful using the script thorpe suggested. This opens you to file-inclusion attacks.

 

Here's how I handled the same thing before moving to a template system:

 


<?php

$allowed_pages = array('page1', 'page2', 'page3', 'page4'); //you'll have to add to this for every new page
$page = $_REQUEST['page'];

if(in_array($page, $allowed_pages))
{
   include($page'.page');
}
else
{
   include('page1.php'); //or some error page. whatever.
}

?>

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.