SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple pages in one?

    I have a few plain old HTML pages that link together and I'd like to use PHP to place them all together in one file ...but still only display each page as the viewer would click the pages relative link. I'm not finding an example of placing PHP into a hyperlink other than like this;
    PHP Code:
    <a href="<?=$_SERVER['PHP_SELF']?>">Link to other content</a>
    which isn't enough info to direct what gets displayed.

    Everytime I try to look something up, the tutorial or example goes off to tell me how to do something even more difficult (Yank-ish style) ...and I'm getting confused. I need to climb one block at a time if someone can assist?

    Thank you.

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have, for example, page1.html, page2.html etc..etc. then you could do something like :
    PHP Code:
    <html>
    <head><title>foo</title></head>
    <body>
    <?php
    //array holding the valid pages (without the .html extension)
    $pages = array(
      
    'page1''page2''page3''page4'
    );
    //loop over the pages and create the links
    foreach($pages as $page){
      echo 
    '<a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'">'.$page."</a><br />\n";
    }
    //if a link was clicked
    if(!empty($_GET['page'])){
      
    //check the page requested is a valid one
      
    if(in_array($_GET['page'], $pages)){
        require_once 
    $_GET['page'].'.html';
      } else {
        echo 
    'Invalid page request';
      }
    } else { 
    //no link was clicked so display the default page
      
    require_once 'page1.html';
    }
    ?>
    </body>
    </html>

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, this is different from the mess I had created, but I don't see where each page would display, I mean the html of each "page". In your example, line 11 doesn't show anything and I wonder if you could confirm the single and double quotes are as you intended? Only because I am trying to break it down logically by matching the pairs and I'm not clear...

    Thank you Mark!

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, the quotes should be fine, and i've tested it locally and it's ok.
    Line 11 just echo's the links and is pretty straight forward, it just loops over the pages array and creates a link. The bits that actually display the html page are the require_once lines. My example does depend on you having ./ in your include_path and that the .html pages are in the same directory as this script.
    You might want to add error_reporting(E_ALL); after the first <?php so it will highlight any problems with your local copy.

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OH! See, I'm trying to have ONE .php file that contains ALL of the content, as in multiple pages are part of this single PHP file. No other external pages, html or includes, just one single file that displays different content based on what is clicked ...a lot like using <DIV> tags and JavaScript to make different content layers visible or invisible ...or another example might be using HTML anchor links with scrollbar turned off (visual example anyway) ...I just want to accomplish this in PHP in order to further understand the basics of PHP multipurpose pages.

    The goal is to only FTP one single file that contains all of these different "pages" and displays them based on visitor selection.

  6. #6
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, i see. Well the theories the same but instead of require_once'ing the files you just display the html.
    Eg.
    PHP Code:
    if(!empty($_GET['page'])){ 
      
    //check the page requested is a valid one 
      
    if(in_array($_GET['page'], $pages)){ 
        
    $callfunc $_GET['page'];
        
    $callfunc(); 
      } else { 
        echo 
    'Invalid page request'
      } 
    } else { 
    //no link was clicked so display the default page 
      
    page1(); 
    }

    .....
    //here's the html bits..
    function page1(){
      
    //echo or display your page1 html here
    }
    function 
    page2(){
      
    //echo or display your page2 html here

    I'd still keep the html in separate files personally as it's far easier to maintain/change and mixing lots of html within PHP can get ugly.

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YES! Yes, yes, yes! At first there was a parsing error on a line, but I matched a brace and it's working well, thank you sir.

    I know what you mean about this not being ideal and potentially a mess for many large pages - thanks for the warning - this is essentially a small cluster of pages that change on rare occasion as part of a contact form and links to map, etc.

    Thanks also for giving me a lead into PHP and functions.

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    NYC
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is how the multipurpose page with email form is shaping up. The first batch of PHP code dictates the "page" displayed. The second batch of code at the bottom of the page, submits the form.
    PHP Code:
    <?php
    ini_set 
    ("display_errors""1");
    error_reporting (E_ALL);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <HTML LANG="en">
    <head>
        <title> Coming Soon </title>
        <META NAME="generator" CONTENT="BBEdit 5.0">
        <META NAME="author" CONTENT="admin M. people">
        <META NAME="copyright" CONTENT="&copy; 2.02K domainname">

    <link rel="stylesheet" href="http://www.domainname.com/css/csstyle.css">

    </head>
    <body bgcolor="#FFFFFF"
          marginwidth="60" marginheight="60" 
          topmargin="60" leftmargin="60" rightmargin="0" 
          text="#FFFFFF" link="#999933" alink="#FF6600" vlink="#FF6600">

                        
        <table bgcolor="#FF3300" border="0" valign="top" cellpadding="0" cellspacing="0">
            <tr>
                <td width="280" height="20" colspan="3" align="center"><img src="http://www.domainname.com/images/spacer.gif"></td></tr>
            <tr>
                <td width="20" height="220" align="center"><img src="http://www.domainname.com/images/spacer.gif"></td>
                <td width="240" height="220" align="center">
                
                    <table bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="240" height="20" align="center"><img src="http://www.domainname.com/images/spacer.gif"></td></tr>
                        <tr>
                            <td width="240" height="220" align="center">
    <?php
    //array holding the valid pages (without the .html extension)
    $pages = array(
      
    'page1''page2''page3''page4'
    );
    //loop over the pages and create the links if you want them to display like a list
    //foreach($pages as $page){
    //  echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'">'.$page."</a><br />\n";
    //}
    //if a link was clicked
    if(!empty($_GET['page'])){
      
    //check the page requested is a valid one
      
    if(in_array($_GET['page'], $pages)){
        
    $callfunc $_GET['page'];
        
    $callfunc(); 
      } else { 
        echo 
    'Invalid page request'
      } 
    } else { 
    //no link was clicked so display the default page 
      
    page1();
    }

    //here's the html bits.. 
    function page1(){
    ?>
    <!-- HTML content to display if condition is true --> 
                <table bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0" width=100%>
                    <tr>
                        <td width="10" rowspan="3"><img src="http://www.domainname.com/images/spacer.gif" width="10"></td>
                        <td height="10"><img src="http://www.domainname.com/images/spacer.gif" height="10"></td>
                        <td width="10" rowspan="3"><img src="http://www.domainname.com/images/spacer.gif" width="10"></td></tr>
                    <tr>
                        <td height="10" align="center"><p>future site of <br><a href="multiPageCS.php?page=page2">Contact Info</a>
    </p><br><p>Coming Soon!</p><br><p>hosting/development by<br>
    <a href="http://www.domainname.com" target="_blank">domainname</a></p></td></tr>
                    <tr>
                        <td height="40"><img src="http://www.domainname.com/images/spacer.gif" height="10"></td></tr>
                </table>

    <?php
    }
    $MailTo 'admin@domainname.com';
        if (
    $BeenSubmitted) {  // this line errors Undefined Variable
            
    if ($MailTo) {
                if (
    mail($MailTo$Subject$Body"From: $MailFrom)) {
                    print (
    "<B><CENTER><FONT COLOR=BLUE>Your email has been successfully sent!</FONT></CENTER></B>\n" );
                } else {
                    print (
    "<B><CENTER><FONT COLOR=RED>Your email was not successfully sent due to a system error!</FONT></CENTER></B>\n" );
                        }
            } else {
                print (
    "<B><CENTER><FONT COLOR=RED>Please enter the recipient's mail to address!</FONT></CENTER></B>\n" );
                    }
        }

    function 
    page2(){
    ?>
    <FORM ACTION="index.php?page=page2" METHOD=POST>
    Your Email Address: <INPUT TYPE=TEXT NAME="MailFrom" SIZE="50"><BR>
    Subject: <INPUT TYPE=TEXT NAME="Subject" SIZE="50"><BR>
    Comments:<TEXTAREA NAME="Body" ROWS="10" COLS="50"></TEXTAREA><P>
    <INPUT TYPE=HIDDEN NAME=BeenSubmitted VALUE=TRUE>
    <INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
    </FORM> 
    <?php
    }
    ?>
                 
                            </td>
                        </tr>
                    </table>

                <td width="20" height="220" align="center"><img src="http://www.domainname.com/images/spacer.gif"></td>
                </td>
            </tr>
            <tr>
                <td width="240" height="20" colspan="3" align="center"><img src="http://www.domainname.com/images/spacer.gif"></td></tr>
        </table>
    </body>
    </html>
    Last edited by chroni; Feb 24, 2004 at 23:12. Reason: fixed error, gained the solution


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
  •