+ Reply to Thread
Results 1 to 4 of 4

Thread: PHP Switch Help

  1. #1
    Sin Zero is offline x10Hosting Member Sin Zero is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    17

    PHP Switch Help

    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 Code:
    <?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 Code:
    <?php
    changelocation 
    ($location);
    function 
    changelocation ($location) {
    echo 
    '<table border="0px" width="850px" cellpadding="0px" cellspacing="0px">
    Outputting the correct pages:
    PHP Code:
    <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)

  2. #2
    LHVWB's Avatar
    LHVWB is offline Lord Of The Keys LHVWB is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Australia
    Posts
    1,308

    Re: PHP Switch Help

    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 Code:
    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.

  3. #3
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: PHP Switch Help

    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.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  4. #4
    Sin Zero is offline x10Hosting Member Sin Zero is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    17

    Re: PHP Switch Help

    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 by Sin Zero; 04-03-2008 at 11:42 AM.

+ Reply to Thread

Similar Threads

  1. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  2. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  3. Sigo con problemas con phpbb2
    By reciecho in forum Soporte
    Replies: 7
    Last Post: 10-20-2007, 06:28 PM
  4. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers