SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do you make a multipurpose page?

    I have a semi-index page that im using as a template. That page just contains a title, and ill probably add some extra things to it in the future. I want to make it so that if the url does not have a variable, it'll load the default page (which is just the title), but if it does have a var, then it will ignore loading the title, and just load the included page.

    PHP Code:
    <?php
    $sec 
    $_REQUEST['sec']
    if ( 
    $sec == NULL ) {
    print 
    "<H1>Title</H1>";
    } else {
    print 
    "include '$sec'";
    }
    ?>
    That is the php in the content part of the page. I am currently getting a error on line 19... which is the line that contains the REQUEST thing.

    PHP Code:
    <a href="index.php?sec=section1">Section 1</a>
    Is how I currently have the links. Is there a way to make the link so that I wont have to include the "index.php" part?
    signature

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sections = array('section1' => 'section1.php''section2' => 'section2.php'); // Define the accessable sections
    $default  'default.php'// The default page
    include (in_array($_GET['sec'], $sections)) ? $sections[$_GET['sec']] : $default

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    [font=Courier,monospaced][color=#000000][color=#0000bb] $sections [/color][color=#007700]= array([/color][color=#dd0000]'section1' [/color][color=#007700]=> [/color][color=#dd0000]'section1.php'[/color][color=#007700], [/color][color=#dd0000]'section2' [/color][color=#007700]=> [/color][color=#dd0000]'section2.php'[/color][color=#007700]); [/color][color=#ff8000]// Define the accessable sections[/color][/color][/font] 
    Sorry, i dont understand what your trying to do over here. Ignore the [font] and [color] tags... i have no idea why its added it.
    signature

  4. #4
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That array contains all sections that are accessable by the 'sec' GET parameter (ultimately, this should be in an DB and not hardcoded).

  5. #5
    SitePoint Addict Messiah's Avatar
    Join Date
    Jun 2001
    Location
    Bloomington, In.
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I understand correctly, than you are wanting have a single index that includes the correct content based on a query string variable? Thats how I have constructed the project in my sig line. Here is how I do it. I have an index.php which contains the header and footer include as well as a content include. Below is my content include:

    PHP Code:
    <?php
    // filename '/includes/content.php'
    // function 'select && display content include based on $page variable'

    // display requested page if valid
    if ( file_exists $_SERVER['DOCUMENT_ROOT'] . "/pages/" $page ".php" ) )
    {
        require ( 
    $_SERVER['DOCUMENT_ROOT'] . "/pages/" $page ".php" );
    }

    // otherwise display custom 404 page
    else
    {
        
    $page "404";
        require ( 
    $_SERVER['DOCUMENT_ROOT'] . "/pages/" $page ".php" );
    }

    ?>

    This method doesnt require an array of allowed pages, instead it checks to make sure the file exists and includes it, otherwise tosses a custom 404 page. Let me know if this is what you meant.
    Messiah | Ink-Press: web publishing simplified!

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, thats exactly what I wanted.. sort of. The only problem is, that I want the default page (without a variable in the url) to display a simple title.
    signature

  7. #7
    SitePoint Addict Messiah's Avatar
    Join Date
    Jun 2001
    Location
    Bloomington, In.
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will do that as well. You just define a default page in the case where the page variable has no value like so:

    PHP Code:
    <?php
    // filename '/includes/content.php'
    // function 'select && display content include based on $page variable'

    // set default page value
    $page $_REQUEST['page'];
    $page = empty($page) ? "home" $page;

    // display requested page if valid 
    if ( file_exists $_SERVER['DOCUMENT_ROOT'] . "/pages/" $page ".php" ) ) 

        require ( 
    $_SERVER['DOCUMENT_ROOT'] . "/pages/" $page ".php" ); 


    // otherwise display custom 404 page 
    else 

        
    $page "404"
        require ( 
    $_SERVER['DOCUMENT_ROOT'] . "/pages/" $page ".php" ); 


    ?>

    Messiah | Ink-Press: web publishing simplified!


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
  •