Jump to content

switches/cases


ItsWesYo

Recommended Posts

I know this can cause a big file size and such, but I want to test something.

 

index.php

<?php
$page = ((isset($_REQUEST['page']))?($_REQUEST['page'])''));

switch($page)
{

case "about": {
echo("about us page");
break;}

case "contact": {
echo("contact page");
break;}

case "link2us": {
echo("link to us page");
break;}

case "affiliates": {
echo("affiliates page");
break;}

default: {
echo("

main page

");
break;}

}

?>

 

 

I am just wondering:

How would I add in mysql stuff?

 

Link to comment
Share on other sites

I've done this using include() in the cases instead of the whole page.  You don't need braces around the case, btw.

 

You can add MySQL functions just like you would normally.  You can add the connection code (connect, select_db) before the switch and page-specific queries in the cases.

Link to comment
Share on other sites

To answer your request as to how I used switch on one of my projects, see the below code.  I trimmed some of the excess code, but that's the basic structure.  Typically I would have a "context" variable and an "action" variable. The context would be matched in the index page (below) and the action would match in the included page with a similar switch structure.  So I could say "index.php?context=reports&action=save" or something, and the 'reports.php' would be included, but within that included file was the input validation and MySQL code that would perform the actual save.

 

<?php

// Session start here

// Functions defined here

// Then include page content based on a GET or POST variable:
if (isset($_REQUEST['context'])) {
    switch($_REQUEST['context']) {
        case 'logout':
            include 'logout.php';
            break;
        case 'home':
            include 'home.php';
            break;
        case 'personal':
            include 'personal.php';
            break;
        case 'reports':
            include 'reports.php';
            break;
        case 'users':
            include 'admin_users.php';
            break;
        case 'quicksearch':
            include 'quicksearch.php';
            break;
        case 'query':
            include 'view.php';
            break;
        default:
            include 'home.php';
    }
} else {
    include 'home.php';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.