Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default many pages in one tutorial

    another one im putting here as me sites tut system still needs upgrading (im in a tutorial mood, rather than working with new db system mood)

    Ok this is a simple tutorial showing how to fit many diffent pages in to a single file.

    Ok, lets say you have 5 or 6 small pages, but you dont want to clutter your website folder with them, and would prefer to have it all done in a single file.

    With PHP this is quite an easy thing to achaive.

    Lets say you have 3 html pages, Page1 page2 and page3, and you want to keep them all in thisfile.php

    Start the php page as your normaly would

    PHP Code:
    <?php
    The next step is to get the varaible carryed in the url, so it knows wich page to display.

    In this exsample the varbople used will be $show
    PHP Code:
    if(!$show){ $show $HTTP_GET_VARS['show']; } 
    What the above does is simply, run the if statment to find out of the varible $show already exists, wich is may on some web server set ups where the varible is set from the url automatiocaly.

    If not, it uses the global varbale $HTTP_GET_VARS to get the varible from the url and set it manualy to $show


    Now we have $show as a varible, the rest is just a simple if stament
    PHP Code:
    if($show=="page1"){ 
    The if stament if($show=="page1") tests to see if the contents of $show were equal to that conatined between the ""in this case page1

    if so it then ddoes whats between then {} tags.

    As these tags can influnce data outside of the php aria, so as not to interfear with the pages html, we can close the php tag

    PHP Code:
    ?> 
    Now it is close the html code for page one would be put here

    The page is the reopened
    PHP Code:
    <?php
    }
    and the closer to the first tag is shut.


    So far you have

    PHP Code:
    </php

    if(!$show){ $show = $HTTP_GET_VARS['show']; }

    if($show=="page1"){
    ?>

    PAge 1 content here, just html as normal

    <?php
    }
    To test for another page we simply carry on the if stament and use, elseis

    PHP Code:
    elseif($show=="page2"){
    ?> 
    Wich simply does the same as before, exspet now the content of page2 is here

    PHP Code:
    }

    <?php
    You would then carry this on to do all the pages so for the 3 pages in the exsample the code would be

    PHP Code:
    <?php

    if(!$show){ $show $HTTP_GET_VARS['show']; }

    if(
    $show=="page1"){
    ?>

    PAge 1 content here,

    <?php
    }
    elseif(
    $show=="page2"){
    ?>

    page 2 content here

    <?php
    }
    elseif)
    $show=="page3"){
    ?>

    and finaly page 3 conetnt here

    <?php
    }
    ?>
    would be the finished code. If we wanted to show a page if on of teh stored pages had not been requested, you would do as before exspet use the else tag

    so you final code would be

    PHP Code:
    </php

    if(!$show){ $show = $HTTP_GET_VARS['show']; }

    if($show=="page1"){
    ?>

    PAge 1 content here,

    <?php
    }
    elseif(
    $show=="page2"){
    ?>

    page 2 content here

    <?php
    }
    elseif)
    $show=="page3"){
    ?>

    and finaly page 3 conetnt here

    <?php
    }
    else{
    ?>

    page for if none is requested

    <?php
    }
    ?>
    Exspet that where it says page 3 content etc, you would actaly have your pages content there.

    You can follow the steps and have as many pages as you like stored in it.

    To display any pages your url would have to how the $show varible

    so in the exsample

    (? turns whats after it in to a request string)

    thisfile.php?show=page1

    would display page1

    and
    thisfile.php?show=page3

    would display page 3

    athogh you did not have to use the variables used in the exsample you could have named the pages what ever you liked, and just had the request string match, say if you had stored a page, wich had in its if or else if tag mrpage

    it would be accesed by thisfile.php?show=mrpage

    this can also be a useful techiqe when writing scripts, such as guest books etc, to keep many differnt code opertains seperate, exspt of cause you would cose the <?php ?> tags dueing them


    I hope this serves to help someone Please post any questions.
    Last edited by Mentor; 10-08-2005 at 08:38 PM.

  2. #2
    Join Date
    Jul 2004
    Location
    Bournemouth. UK
    Posts
    3,638
    Tokens
    0

    Latest Awards:

    Default

    Woah. Great Tutorial man. I never knew that could be done
    REMOVED

    Edited by jesus (Forum Super Moderator): Please do not have text in your signature which is over size 4.

  3. #3
    JoeComins Guest

    Default

    Thnkx Mentor. Great advice again

  4. #4
    Join Date
    Jul 2004
    Location
    Webby Forums!
    Posts
    1,879
    Tokens
    0

    Latest Awards:

    Default

    get into database fixing mode dammit !!!11 But great tut


    Chilimagik.net // Reviews, Band Biographies, News, Pics + Loads More!!
    [Thybag.co.uk - Vive la revolutione]

  5. #5

    Default

    Hey,

    You know when you start the php, you used
    Code:
    </
    is that sopose to be
    Code:
    <?
    ??
    TheGSP
    :8

  6. #6
    Join Date
    Oct 2004
    Posts
    12
    Tokens
    0

    Default

    Quote Originally Posted by AlloyHosting
    Hey,

    You know when you start the php, you used
    Code:
    </
    is that sopose to be
    Code:
    <?
    ??
    Yea It Sopose to be this
    PHP Code:
    <?

  7. #7

    Default

    Hey,

    That's ok then. I thought i was making errors in current scripts like this one.
    TheGSP
    :8

  8. #8
    Join Date
    Jun 2005
    Location
    USA
    Posts
    2,047
    Tokens
    0

    Latest Awards:

    Post

    Great tut
    10/10

  9. #9
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default

    Quote Originally Posted by AlloyHosting
    Hey,

    You know when you start the php, you used
    Code:
    </
    is that sopose to be
    Code:
    <?
    ??
    Oh yea sorry didnt notice that, must have missed my shift. fixed now

Posting Permissions

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