Results 1 to 10 of 10

Thread: Multiple pages in one PHP doc.

  1. #1
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Multiple pages in one PHP doc.

    i was looking earlier, and failing to properly imprement Tweys mysql login code.
    (http://www.twey.co.uk/?q=loginscript)
    and i was wondering how exactly the different pages were shown while keeping the php document the same?
    i know that the
    <?php
    }

    function foot() {
    ?>
    peice echos the other pages and that
    <?php
    foot();
    }
    /*rest of code*/
    defines the start of a page, but i have no clue as to how the code is placed. this has been bugging me for a while... oh, and i would like to beable to put my entire site like that, just without the login funtions. would make my time on my website alot better spent.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Just echo different code depending on circumstances. I took this to extremes with the chat app I wrote on these forums a while back, which uses PHP and XHR, and which I shoved into one file, images and all.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    <?php
    if ($_GET['page'] == "1") {
    echo "<html>...this is page one...";
    }
    elseif ($_GET['page'] == "2") {
    echo "....this is page two...";
    }
    else {
    echo "...this is the default index page if there either is no value of ?page= or if it's value isn't already dealt with in the script, so they don't get a page not found error.";
    }
    ?>


    Also, you can use a switch statement to send it to functions... basically like above, but it calls a function instead of doing the action right there. This has it's uses too.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    awsome.
    its allright to replace the double quotes around the echoed code with single ones right?
    mostly because my website uses double quotes often and i read that
    echo "<a href="url">"
    would get you <a href= and then an error. (because the first quote interpreted as start and the second is interpreted with end)
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you're echoing large blocks of HTML, you can drop out of PHP parsing mode altogether:
    Code:
    <?php if(isset($_GET['page']) && $_GET['page'] === '1') { ?>
    
      <html>
        <!-- Page 1 -->
      </html>
    
    <?php } else if(isset($_GET['page']) && $_GET['page'] === '2') { ?>
    
      <html>
        <!-- Page 2 -->
      </html>
    
    <?php } else { ?>
    
      <html>
        <!-- Default -->
      </html>
    
    <?php } ?>
    djr33: elseif is for lazy people.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default well, i tried

    djr33's code and got this
    Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in C:\UTILITIES\Webserv\Abyss Web Server\htdocs\web.php on line 30

    and this is line 30

    <li><a href="Installers/RobotArena2.exe">Robot Arena</a>

    so now im trying yours...and it works. thank you very much , Twey, you have helped me once again.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There's no error in my code, but adding double quotes inside the quotes exits the quotes.

    To use double quotes, yes, use single quotes around them. However, sometimes, you may need both in a string, so you can use either, then escape those quotes that match the outside ones.
    For example: echo "<img src=\"image.jpg\">";
    The backslashes escape them so they aren't used in the php but rather output to html. (The backslashes are just a php command... they aren't output to the html.)

    And, yes, Twey's method works, but only if you're looking for lots of text. If you just want a couple lines of text, might want to use the other syntax. either is fine.... just that knowing both will help in the end.


    Twey, why is elseif lazy?
    It continues the same set of ifs. That means that ONLY if the first fails will the elseif execute. If the first is true, then the second won't execute (like if there was some weird way of setting the variable to two values, or if they weren't mutually exclusive for some reason), and that has been very helpful for me.
    It's just like else { if (...) {.....}}, but less to type.
    What's your alternative?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    elseif is a separate keyword that performs exactly the same function as a combination of "else" and "if." The braces are, as always, optional;
    Code:
    if(1) {
    
    } else if(2) {
    
    }
    is perfectly valid. There's really no need for elseif (heck, it saves a single character), and it's a bad habit into which to get, since most C-like languages don't bother implementing something so pointless.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, didn't realize you can just put a space there.

    But if it's there, why not use it?

    But.... yeah, I see your point.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Since the whole of the second if block counts as a single statement, there's no need to enclose it in braces.
    But if it's there, why not use it?
    Quote Originally Posted by myself
    it's a bad habit into which to get, since most C-like languages don't bother implementing something so pointless.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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