Jump to content

[SOLVED] Switch statement not functioning in URL


nirvana4lf

Recommended Posts

I was trying to run a switch statement with open url and its not working. the statement runs correctly when the variable is embedded in the script. i'm running this from a home computer with XAMPP (comes with Apache 2.2 and PHP 5.2.3 among other things). Whats the problem here? Does it have something to do with Apache or PHP? Thanks.

Link to comment
Share on other sites

no i guess im using the wrong term. here is what i mean (i found this code from an old band site i had to see if i was just doing something wrong and i wasnt):

 

<?php
function sam() {

    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>Sam - Guitar</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}

function marc() {
    echo "<center><h1>Band Members</h1></center><br><br>";    
    echo "<center><h2>Marc - Guitar</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}

function jordan() {
    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>Jordan - Vocals and Bass</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}


function jake() {
    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>Jake - Drums</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}


function BandMembers() {
    echo "<center><h1>Band Members</h1></center><br><br><br><br>";
    echo "<a href=\"switch_test.php?func=sam\">Sam H.</a><br><br>";
    echo "<a href=\"switch_test.php?func=marc\">Marc F.</a><br><br>";
    echo "<a href=\"switch_test.php?func=jordan\">Jordan B.</a><br><br>";
    echo "<a href=\"switch_test.php?func=jake\">Jake S.</a>";
}

switch($func) {

    default:
    BandMembers();
    break;
    
    case "sam":
    sam();
    break;

    case "marc":
    marc();
    break;
    
    case "jordan":
    jordan();
    break;
    
    case "jake":
    jake();
    break;

}

?>

I also have a domain with SBC Yahoo! and this script works there but not on my server at home. Sounds like a configuration problem?

Link to comment
Share on other sites

You probably have Registered_Globals on on one server, and off on the others.

 

Do this:

 

<?php
$func = $_GET['func'];

function sam() {

    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>Sam - Guitar</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}

function marc() {
    echo "<center><h1>Band Members</h1></center><br><br>";    
    echo "<center><h2>Marc - Guitar</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}

function jordan() {
    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>Jordan - Vocals and Bass</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}


function jake() {
    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>Jake - Drums</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";

}


function BandMembers() {
    echo "<center><h1>Band Members</h1></center><br><br><br><br>";
    echo "<a href=\"switch_test.php?func=sam\">Sam H.</a><br><br>";
    echo "<a href=\"switch_test.php?func=marc\">Marc F.</a><br><br>";
    echo "<a href=\"switch_test.php?func=jordan\">Jordan B.</a><br><br>";
    echo "<a href=\"switch_test.php?func=jake\">Jake S.</a>";
}

switch($func) {

    default:
    BandMembers();
    break;
    
    case "sam":
    sam();
    break;

    case "marc":
    marc();
    break;
    
    case "jordan":
    jordan();
    break;
    
    case "jake":
    jake();
    break;

}

?>

Link to comment
Share on other sites

By the way, you can make a single function that will replace all the individual band members functions. Just do this:

 

<?php

function memberInfo($name, $position){
    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>$name - $position</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";
}

?>

 

Then to call the function, you would just do this:

 

memberInfo("Sam", "Guitar");

 

Just a suggestion, not sure if it will work with what your trying to do.

 

Link to comment
Share on other sites

wait, now it works... weird. thanks for the idea, but im not using it in this context. this is just some old script i found that i knew applied to what i'm doing right now. thank you very much!

 

By the way, you can make a single function that will replace all the individual band members functions. Just do this:

 

<?php

function memberInfo($name, $position){
    echo "<center><h1>Band Members</h1></center><br><br>";
    echo "<center><h2>$name - $position</h2></center><br><br>";
    echo "<a href=\"switch_test.php\">Back</a>";
}

?>

 

Then to call the function, you would just do this:

 

memberInfo("Sam", "Guitar");

 

Just a suggestion, not sure if it will work with what your trying to do.

 

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.