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
https://forums.phpfreaks.com/topic/57744-switchescases/
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
https://forums.phpfreaks.com/topic/57744-switchescases/#findComment-285896
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
https://forums.phpfreaks.com/topic/57744-switchescases/#findComment-285963
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.