PHP Switch Help

Sin Zero

New Member
Messages
17
Reaction score
0
Points
0
I am using switches to display pages on my index page, where my url is (http://) anime.centuragate.exofire.net/index.php?act=PAGE . I am having difficulties setting it up to work so that I can have a sub-page for the other ones like so (http://) anime.centuragate.exofire.net/index.php?act=PAGE&sub=SUBPAGE .

Here are some code snippets
To get the url and setup the text for the title tag:
PHP:
<?php
$location=$_GET['act'];
if (empty($location)) {
    $location='home';
}
if ($location=='home') {
    $title='Home';
}
elseif ($location=='directory') {
    $title='Directory';
}
elseif ($location=='section_feed') {
    $title='Section RSS';
}
?>
Setting up the function and outputting the table:
PHP:
<?php
changelocation ($location);
function changelocation ($location) {
echo '<table border="0px" width="850px" cellpadding="0px" cellspacing="0px">
Outputting the correct pages:
PHP:
<td class=content>
<center>
<script type="text/javascript" src="http://x10hosting.com/adserve.js?corporate"></script>
</center>';
switch ($location) {
    case 'home':include ('main.php'); break;
    case 'directory':include ('directory.php'); break;
    case 'section_feed':include ('section_rss/index.php'); break;
}

echo '</td>
How would I be able to set it up so I can work with sub-pages according to the page it is associated with? (I hope that sentence made some sense. Hehe)
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
You could have another $_GET variable, such as sub_act, this would mean that when you are checking for cases, within each act case you can also check to see if a sub_act has been requested or you can just display the index of that action if none has been sent.

Do this before you break in the case, eg.
PHP:
switch ($location) {
    case 'home':
         switch ($sub_location){
             case 'sub1':include ('sub1');break;
             case 'sub2':include ('sub2');break;
             case 'sub3':include ('sub3');break;
                 // Else option.
             include ('main.php'); break;
         };
    break;

    case 'directory':include ('directory.php'); 

    break;
    case 'section_feed':include ('section_rss/index.php'); 

    break;
}

Note: My code is probably slightly wrong because I haven't really worked with switches (I usually just use if, elseif and else statements) but you should get the idea.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
The only problem with that is that the switch's else clause should be 'default:'. Without that, the two statements at the end of the inner switch would be considered to be part of case 'sub3'.

Also, the semi-colon after the inner switch's right curly brace is unneeded, but I don't believe it would cause any sort of error.
 

Sin Zero

New Member
Messages
17
Reaction score
0
Points
0
Alright. I'm understanding what verbsite is saying, but I am unsure of what woiwky means by the clause 'default:'.

I have tried it and understand what it should do but there is a problem with verbsite's code and I don't know what to do.

I have also tried turning the sub-switch into a function as well but can't figure out how to make it so that it displays the sub-switch only and not the previous page as well.

I will keep trying to figure it out on my own, but other solutions would be welcome too.

EDIT: I figured it out by having the sub-switch defined as a function. Rep for both of you since you helped me figure it out.
 
Last edited:
Top