Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 7 1 2 3 ... LastLast
Results 1 to 10 of 63
  1. #1
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    I haven't had an opportunity to test this code yet. If you get errors, please reply.

    Requirements: PHP for all methods, MySQL for third method
    Time Required: Varies; increases per method

    There are three simple ways that you can make one page display multiple pages. For example, you could have index.php display your site's index page, index.php?action=privacy to show a privacy form, and index.php?action=contact to display an e-mail form.

    Method 1: switch Statement

    The switch statement in PHP is like the if statement in that it evaluates an expression and then executes a block of code as according to the expression.

    Here's how you could display different content per page:
    Code:
    switch ($_GET['action'])
    {
        case 'privacy': // privacy form (index.php?action=privacy)
            ?>
            HTML for the privacy form goes here
            <?
            break;
        case 'contact': // e-mail form (index.php?action=contact)
            ?>
            HTML for the contact form goes here
            <?
            break;
        default: // default page (index.php with no "?action=")
            ?>
            HTML for the default page
            <?
    }
    Method 2: Flat Files

    You can repeat the process above but get the HTML from actual HTML files instead.
    Code:
    switch ($_GET['action'])
    {
        case 'privacy': // privacy form (index.php?action=privacy
            // Inserts the contents of privacy.html in the current directory
            include('privacy.html');
            break;
        case 'contact': // e-mail form (index.php?action=contact
            include('contact.html');
            break;
        default: // default page (index.php with no "?action=")
            include('index.html');
    }
    For example, going to index.php?action=privacy would display the contents of the file privacy.html in the same directory as index.php. An error is displayed if the file can't be found.

    Method 3: Retrieve from Database

    This last method is the most complex but holds the best advantage: it lets you edit the HTML code without editing the actual source code of a file. With a tool such as phpMyAdmin (http://www.phpmyadmin.net/) you can edit the HTML remotely.

    This method assumes that your database server is "localhost", the database name "testing", the username "root", and a blank password.

    First, run this query to create the necessary table:
    Code:
    CREATE TABLE indexhtml
    (
        id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        action VARCHAR(100) NOT NULL,
        html TEXT
    );
    Here is the PHP code for index.php:
    Code:
    $link = mysql_connect('localhost', 'root', '') or die('Could not connect to db server');
    mysql_select_db('testing') or die ('Could not select db');
    
    // SQL injection fix
    $allowedactions = array('privacy', 'contact', '');
    $action = $_GET['action'];
    if (!in_array($action, $allowedactions) && !empty($action))
    {
        echo 'Won\\'t open page; SQL injection possible';
    }
    else
    {
        if (empty($action))
        {
            // in the db, index.php with no action is stored with the action of 'default'
            $action = 'default';
        }
        $result = mysql_query("SELECT html FROM indexhtml WHERE action='$action'");
        if (!$result || mysql_num_rows($result) == 0)
        {
            echo "HTML for \"$action\" not found";
        }
        else
        {
            $s = mysql_fetch_row($result);
            echo $s[0];
        }
    }
    This code is obviously more complex than the previous two examples, so here is pseudocode of the above:
    Code:
    connect to the MySQL server or end the script with an error message
    select the database or end the script with an error message
    
    // SQL injection fix
    define allowed actions (i.e., the text after "?action=" in the URL)
    cache the action to a variable
    if the action is not listed in the allowed actions and is not empty
    {
        echo an error message
    }
    else
    {
        if action is empty (i.e., the user viewed just plain index.php)
        {
            make the action 'default'
        }
        run a query to select the HTML from the table for the given action
        if the query failed or returned nothing
        {
            echo an error message
        }
        else
        {
            return the first (and hopefully only) row from the results
            echo the first column of the row (only column is the HTML)
        }
    }
    If you have questions, just reply.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  2.  

  3. #2
    Member
    Join Date
    Apr 2002
    Location
    Oxford, UK
    Posts
    32
    Member #
    61
    Filburt

    I used you code but, i would like it to point to a different page if i use ur code and where it puts html i put the url to the page it just displays the path at the top of the page.

    Pete
    Peter

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Run that by me again?
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    Member
    Join Date
    Apr 2002
    Location
    Oxford, UK
    Posts
    32
    Member #
    61
    Filburt

    I have different pages with different designs on them but i would like the link so show up short

    if i had a link called about but it was in

    http://www.domain.com/files/areas/contact/1/index.php

    how could i just show it as

    http://www.domain.com/index.php?action=about??

    the above does not work.

    Peter
    Peter

  6. #5
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    It should work:
    Code:
    switch ($_GET['action'])
    {
        case 'about':
            include('files/areas/contact/1/index.php');
            break;
        default: // default page (index.php with no "?action=")
            include('index.html');
    }
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  7. #6
    Member inthezone's Avatar
    Join Date
    Sep 2002
    Posts
    59
    Member #
    192
    I keep getting errors about the HTML for "default" not being found.

    Am I typing the URL correctly?: /page.php?action=about=0
    The Extent of my PHP Knowledge:

    <?php
    print "In the Zone";
    ?>

  8. #7
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    page.php?action=about
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  9. #8
    Member inthezone's Avatar
    Join Date
    Sep 2002
    Posts
    59
    Member #
    192
    Also, say I create queries for different id #s under about...how would I access those via the web?

    BTW, do you know of a PHP script that displays the number of queries a page uses? Thx
    The Extent of my PHP Knowledge:

    <?php
    print "In the Zone";
    ?>

  10. #9
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    First question: aroo?
    Second question: there isn't one really, but you can write a wrapper:
    Code:
    $querycount = 0;
    
    function mysql_query_inccounter($query)
    {
        global $querycount;
        $querycount++;
        return mysql_query($query);
    }
    Then call mysql_query_inccounter instead of mysql_query and $querycount will contain the number of queries you made.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  11. #10
    Member inthezone's Avatar
    Join Date
    Sep 2002
    Posts
    59
    Member #
    192
    Originally posted by filburt1
    First question: aroo?.
    I mean if I create something with the "about" action, and list "1" as the id instead of leaving the id blank.
    The Extent of my PHP Knowledge:

    <?php
    print "In the Zone";
    ?>


Page 1 of 7 1 2 3 ... LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 01:26 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com