✄codebook

a cut&paste code archive

PHP Switch...Have URLS like: (page).php?id=(subpage)

04/12/08 02:46 PM

Resources

  1. php.net/switch
  2. php.net/print
  3. w3schools

Scenario

Create a php page with dynamic elements where URLs of this format page.php?id=section accesses different parts of a single file.

Solution

step1

Declare this in your PHP file between <head> and </head>:

where page will be the url trailer that will trigger which parts of the page to load.
You may rename page to anything else you like (but follow the renaming to the steps below or nothing will work).

step2

Let’s assume our “host page” is index.php.

This is the code that tells the “host page” which section to display depending on the url trailer:

with the codes above, index.php will display, where that code is placed, the contents of 0.txt.
However, index.php?page=A will display, where that code is placed, the contents of 1.txt,
and index.php?page=B will display, where that code is placed, the contents of 2.txt, and so on.

The files that you require does not need to be .txt, can be .htm, .php, or any text file. Basically the contents of which text file will be taken and replaced with that section, dynamically.

Alternative ways to use the switch code

Using the echo ""; function:

Using the print(''); function:

Using the print $var; function:

Using the print(''); function:

View the demo here.

Tags: PHP


Comment

  1. Большое спасибо!

    heloooo · 13/05/09 05:10 PM · #

  2. Очень интересно было познакомиться с материалом! Вы – молодец.

    — Visitor · 29/05/09 05:10 PM · #

  3. it errored out on this line

    $thispage = $parts[count($parts) – 1];

    any idea?

    — chris · 19/09/09 06:53 PM · #