+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 20

Thread: [Tutorial PHP] Switching Content / Including Pages

  1. #1
    x10 Elder DarkDragonLord is an unknown quantity at this point DarkDragonLord's Avatar
    Join Date
    Mar 2007
    Location
    Brazil
    Posts
    782

    Talking [Tutorial PHP] Switching Content / Including Pages

    Greetings!

    Welcome to my PHP Tutorial!
    Suggestions are welcome :D

    Here we will learn a way to include your pages in the same layout, in a easy way.

    I'll show all code then comment:

    PHP Code:
    <?php
    if ($_GET['ddl'])
    if (
    file_exists('./'.$_GET['ddl'].'.html'))
    @include(
    $_GET['ddl'].'.html');
    else
    @include(
    './error.html');
    else
    @include (
    "./main.html");
    ?>
    I'll explain what it does than i'll break in.

    Insert this in your index.php. If you go to yourdomain.com, it will open index.php and, since no variable is set, it show us main.html. So main.html will be your opening page.
    Then, if you make any link as following yourdomain.com/index.php?ddl=something , the code will read the variable ddl and get the "something" name. Then, it will search in the folder for a something.html and include in the place main was. But if something.html is not found, it will replace main.html with a 404.html.

    Now, lets break it!


    PHP Code:
    <?php
    if ($_GET['ddl'])
    (...)
    else
    @include (
    "./main.html");
    ?>
    This is our main code. If ddl is set, it goes to the sub-code. If it is not set, it include main.html. This code only happens when you have yourdomain.com/index.php , that means, with no variable ddl.

    When you create a link, you should make it as index.php?ddl=nameofpage

    the ? after the .php says to the script that is a variable. Then, = is to tell what is the value of the variable.

    Now the sub-code. It is triggered when if ($_GET['ddl']) detects a variable.

    PHP Code:
    if (file_exists('./'.$_GET['ddl'].'.html'))
    @include(
    $_GET['ddl'].'.html');
    else
    @include(
    './error.html'); 
    If the 'ddl' . html exists, include it. Else, show us a error page.

    As you can see, this version is a little better and simple to work than coolv1994's one, since we do not need to declare all pages on it (imagine a website with more than 50 pages O.o.. geez).

    NOTE1:
    Those @ before the include, prevent those PHP error logs like those:
    Code:
    Warning: include(./error.html) [function.include]: failed to open stream: No such file or directory in /home/XXXX/public_html/index.php on line yy
    
    Warning: include(./error.html) [function.include]: failed to open stream: No such file or directory in /home/XXXX/public_html/index.php on line yy
    
    Warning: include() [function.include]: Failed opening './error.html' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/XXXXX/public_html/index.php on line yy
    That means, if the code dont have @ and the script generated an error log like those, EVERYONE will see your login name in the host. (the XXXX are the login name). And yy is the line that generated the error.

    Using it without @ is good when you are debugging, when the page dont not include and you are sure you are right :P

    NOTE2:
    the 'ddl' can be changed to anything you want. Just be sure to change your link's as well (index.php?NameOfVariable= )

    Hope you like it.
    You can found more tutos at
    http://ddl.exofire.net
    (implementing tutorial pages this week)




    A FULLY EXAMPLE:
    Question:
    Very nice but how do you put in the actual file name, does it automatically search for it?
    Yes, automatic search,you just need to say the name of the HTML, as you can see in the script,
    PHP Code:
    @include($_GET['ddl'].'.html'); 
    it will get the value of ddl you said in the URL and automatic add a .html, then it will find this "valueOfDdlyouEnter.html"

    I'll show you using the template i just made:

    It have the following files.

    index.php
    dummy1.html
    404.html
    main.html

    When you go to the webpage ( http://ddl.exofire.net/temp/index.php ), it include the main.html, since in the url, no ?ddl= was found.
    Then, if you click in the links, you can see that is index.php?ddl=dummy1
    When you click on it, the script see that now have a value for ddl and then, look in my folder for a dummy1.html
    Then, it include the dummy1 and show you.

    I made an example with the link "terceiro", linking to dummy2 (but as you can see in my files, there is none dummy2.html). So, when you click in the index.php?ddl=dummy2, the script check for a dummy2.html. Since it will not find any, it includes the 404.html (in the tutorial, error.html).

    The good things i think that is including HTML files, not PHP files are:
    * Easy to manage - Check the file icons. Green for PHP (Dreamweaver) and blue for HTML (IE) so i can easy find what is what;
    * Even as HTML files, you CAN add PHP codes on it. Why? Well, the include is simple get all code in the html, and add it in the .PHP, this way, even as HTML, when the file is included, it become a php file.


    Here are examples of the html you need to include:
    (the ID and classes i added, is just for formatting them in the CSS)
    dummy1.html
    Code:
    <div id="phpinclude">
    <h1 class="dest">Title Here</h1>
    <p> Lorem ipsum dolor!. </p>
    <p> Lorem ipsum dolor!. </p>
    </div>
    404.html / error.html
    Code:
    <div id="phpinclude">
    <h1 class="dest">Error Page</h1>
    <p> This is an ERROR PAGE. </p>
    <p> Will show only if the URL is wrong. I've done a link with wrong url so you can check this error page. </p>
    </div>
    main.html
    Code:
    <div id="phpinclude">
    <h1 class="dest">Welcome to Template #1 by Raphael DDL</h1>
    <p> This is a FREE Template provided by .dragon//CREATIVE Website. </p>
    <p> Report imediatly if you have purchased or downloaded this template other than ddl.exofire.net</p>
    <p>Regards,<br /> Raphael DDL.</p>
    </div>
    Now go http://ddl.exofire.net/temp/index.php and test

    As you can see, you DO NOT NEED to add the <html> <body> etc tags.. They already are in the .php. These html are not really html files (for browsing). They are just containers with code and the script will make the 'body replacement', adding the content of the html you ask.

    So, if you want to test my theory, change this line:
    include($_GET['ddl'].'.html');

    with this one:
    @include($_GET['ddl'].'.txt');

    Then, create a dummy1.txt and test the url index.php?ddl=dummy1 in your host. As you can see, those html codes inside the txt will become part of the php page.


    I hope i explained well enough to understand :D
    Last edited by DarkDragonLord; 01-28-2008 at 01:20 PM.
    Regards,
    Raphael DDL

    Designing Solutions for You
    *Web Design;
    *Coding;
    Free Downloads;
    and all related Stuff
    .


    My Tutorials:
    | Multi-Language Websites | Rotative Banners |
    | Bookmark Script for All Browsers
    |
    |
    PHP Switching/Including Content|
    |


  2. #2
    x10 Elder DarkDragonLord is an unknown quantity at this point DarkDragonLord's Avatar
    Join Date
    Mar 2007
    Location
    Brazil
    Posts
    782

    Re: [Tutorial PHP] Switching Content / Including Pages

    Hmm.. Just to know. This tuto was useful for someone? Because i always see people using php but declaring each file in arrays so, its kinda boring. My method is a quite simple.
    Regards,
    Raphael DDL

    Designing Solutions for You
    *Web Design;
    *Coding;
    Free Downloads;
    and all related Stuff
    .


    My Tutorials:
    | Multi-Language Websites | Rotative Banners |
    | Bookmark Script for All Browsers
    |
    |
    PHP Switching/Including Content|
    |


  3. #3
    Lord Of The Keys LHVWB is an unknown quantity at this point LHVWB's Avatar
    Join Date
    Jan 2008
    Location
    Australia
    Posts
    1,308

    Re: [Tutorial PHP] Switching Content / Including Pages

    Quote Originally Posted by DarkDragonLord View Post
    Hmm.. Just to know. This tuto was useful for someone? Because i always see people using php but declaring each file in arrays so, its kinda boring. My method is a quite simple.
    I completely agree with you, I am currently making a tutorial and mod for SMF, using a similar method that will allow you to access all files within a certain folder by using the already created action array that SMF uses. Its uncompleted but have a look anyway, notice the ?action=tutorial which I use.

    http://verbsite.x10hosting.com/index...ction=tutorial

    Edit: I'll finish the tutorial and the mod, and make a post about it in a day or two.
    Last edited by LHVWB; 03-27-2008 at 07:23 AM.

  4. #4
    Community Advocate diabolo is on a distinguished road diabolo's Avatar
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,553

    Re: [Tutorial PHP] Switching Content / Including Pages

    how would you go about retrieving file from a different directory?
    since ?dll=dir1/dir2/page1
    looks ugly, and the browser might read it wrong?
    please add to my reputation if you found my post helpful, it gets me closer to free stuff.

    Check out my Tutorials
    [JS] Load Object Last | [php][GD] Creating A Dynamic Signature

  5. #5
    x10 Sophmore BorderLineSigs is an unknown quantity at this point BorderLineSigs's Avatar
    Join Date
    Mar 2008
    Posts
    232

    Re: [Tutorial PHP] Switching Content / Including Pages

    nice tut...but it was to long...i got bored half way through it xP...try to shrink it a bit next time(or put some pictures in...not just words and codes...doesn't look very attractive =P)
    Nice Site Here
    Great site to vistit: http://gamez.exofire.net
    give rep+ if my post helped AT ALL or you will pay...LATE FEES

  6. #6
    x10 Elder DarkDragonLord is an unknown quantity at this point DarkDragonLord's Avatar
    Join Date
    Mar 2007
    Location
    Brazil
    Posts
    782

    Re: [Tutorial PHP] Switching Content / Including Pages

    Quote Originally Posted by diabolo View Post
    how would you go about retrieving file from a different directory?
    since ?dll=dir1/dir2/page1
    looks ugly, and the browser might read it wrong?

    Hmm.. Yeah, its ddl=dir1/dir2/page1
    Yeah, looks ugly lol.
    To my portfolio, i made a new variable.


    So, when someone goes to ?ddl=porfolio ,it calls portfolio.html right? In portfolio.html, i added
    Code:
    <?php
    if ($_GET['ports'])
    if (file_exists('./portfolio/'.$_GET['ports'].'.html'))
    @include('./portfolio/'.$_GET['ports'].'.html');
    else
    @include('./404.html');
    else
    @include ("./portfolio_menu.html");
    ?>
    and then, i created a portfolio_menu.html, that have all links to the portfolios, like 'videos', 'ads' etcetera Like this:
    Code:
    <a href="?ddl=porfolio&ports=videos" />videos</a>
    Now Explaining: When you go to ?ddl=portfolio, it checks if have a ports variable in the URL. If have, goes to portfolio folder and search for the HTML with the name declared at 'ports'. If the file exists, it include, if not exist, it include 404. But when no 'ports' variable is found at URL, it calls portfolio_menu, which is the one with the links.



    So, what looks ugliest?
    yoursite.com/?ddl=dir1/dir2/page1
    or
    yoursite.com/?ddl=page1&ports=page2


    Lol. And no, no problem, browsers does not understand wrongly. I use the &ports because when i manage to understand how Friendly URL's work, will be easier to make them 'more friendly' than dir1/dir2/page1 (will look like xxx.com/portfolio/videos for example)





    Quote Originally Posted by BorderLineSigs View Post
    nice tut...but it was to long...i got bored half way through it xP...try to shrink it a bit next time(or put some pictures in...not just words and codes...doesn't look very attractive =P)


    The tutorial was little. But i used an answer i gave to a possible question users might have (another board asked :P)











    AH, an important thing: I was checking my Logs and someone was trying to make his/her homepage shows inside mine, maybe trying to hack or dunno what.... But, since i used if (file_exists, he wasn't able to do anything lol. because the url he was using does not existed in MY host. :D

    example: my website, when someone is viewing the portfolio is: www.raphaelddl.com/?ddl=portfolio&lang=en He tried change the variable of ddl to an URL [im just copying from the log, do not try go in there. Might be virus, dunno] like this: http://raphaelddl.ddl.exofire.net/?d...2F&amp;lang=en

    he even tried change the &lang= too but not sucessful since my language system just apply IF lagn is en or pt, nothing more lol :D

    So, all tries it did just gave him the 404.html hahahahahah
    Last edited by DarkDragonLord; 03-28-2008 at 01:18 PM.
    Regards,
    Raphael DDL

    Designing Solutions for You
    *Web Design;
    *Coding;
    Free Downloads;
    and all related Stuff
    .


    My Tutorials:
    | Multi-Language Websites | Rotative Banners |
    | Bookmark Script for All Browsers
    |
    |
    PHP Switching/Including Content|
    |


  7. #7
    x10 Sophmore ThePaintGuru is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    208

    Re: [Tutorial PHP] Switching Content / Including Pages

    This is an excellent tutorial, albeit for a simple concept. Brings back memories, too. The first program I wrote in Perl (hadn't heard of PHP yet) was a "CMS" that worked basically the same way. Can be quite useful if you're looking to make an easily-customizable static website.
    ----------
    If you found my post helpful in some way, I'd really appreciate a few credits as I'm saving up for a domain.

    You can click the blue check mark in the upper right corner of this post to increase my reputation.

  8. #8
    x10Hosting Member Joker169 is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    1

    Smile Re: [Tutorial PHP] Switching Content / Including Pages

    can be useful

  9. #9
    -¤† RockStar †¤- Punk is an unknown quantity at this point Punk's Avatar
    Join Date
    Jan 2008
    Location
    University
    Posts
    5,817

    Re: [Tutorial PHP] Switching Content / Including Pages

    i have one problem here T.T how i can put more than one page?
    Can you giveme one example in quote how here
    <?php
    if ($_GET['ddl'])
    if (file_exists('./'.$_GET['ddl'].'.html'))
    @include($_GET['ddl'].'.html');
    else
    @include('./error.html');
    else
    @include ("./main.html");
    ?>
    but whit more than 3 webs please ^^


    thankyou ^^
    grettings
    salu2
    zkDev: (Proffesional Web Designs)
    http://zkDev.co.cc
    PTC Society: (Earn Money Online)
    http://PTCSociety.co.cc

    Get $0.20 (Next 10 Downloads) INSTANTLY to Your PayPal (Earn UNLIMITED)
    Payment Proofs by Users


    Si mi mensaje fue UTIL, Por Favor Clic en: (Parte Inferior Izquierda de mi Mensaje)
    If my message was USEFUL, Please Click in: (Bottom Left of my Post)




  10. #10
    x10 Elder DarkDragonLord is an unknown quantity at this point DarkDragonLord's Avatar
    Join Date
    Mar 2007
    Location
    Brazil
    Posts
    782

    Re: [Tutorial PHP] Switching Content / Including Pages

    lol?

    make the index with all the layout. And then,put the code in the place the content have to be shown.

    create a main.html, with things to be shown when ppl enter in your website. Create a 404.html just for when ppl put wrong links and then, x number of html pages with JUST content.

    then the link will be xxx.com/index.php?ddl=NAMEofTheHTMLhere

    just that.
    Regards,
    Raphael DDL

    Designing Solutions for You
    *Web Design;
    *Coding;
    Free Downloads;
    and all related Stuff
    .


    My Tutorials:
    | Multi-Language Websites | Rotative Banners |
    | Bookmark Script for All Browsers
    |
    |
    PHP Switching/Including Content|
    |


+ Reply to Thread
Page 1 of 2
1 2 LastLast

Similar Threads

  1. Urgent help please!
    By retro-bliss in forum Free Hosting
    Replies: 2
    Last Post: 10-25-2007, 03:24 PM
  2. help-add content to all pages at once
    By andhrafun in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 09-19-2007, 03:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts