Jump to content

[SOLVED] conditional statement for default case???


scarhand

Recommended Posts

I'm stumped with this. I'm trying to create a conditional statement for my menu that will check to see if the page is on the default case.

 

Here is my switch code (this works fine):

 

switch($_GET['p']) 
{
  case 'about':
    AboutPage();
    break;
    
  case 'rules':
    RulesPage();
    break;
    
  case 'join':
    JoinPage();
    break;
    
  case 'files':
    FilesPage();
    break;
    
  case 'donate':
    DonatePage();
    break;
    
  case 'staff':
    StaffPage();
    break;
    
  case 'wiki':
    SoonPage();
    break;
    
  default:
    IndexPage();
}

 

Heres the code I can't seem to get to work:

 

    <?php
    if (is_null($_GET['p']))
    { echo "<td class=\"topmenu\"> \n"; }
    else
    { echo "<td class=\"topmenu2\"> \n"; }
    ?>
      <a href="#">INDEX</a>
    </td>

There is no need for break in the default case.

 

Can you try adding a simple statement like "print 'hello, i am the default case'; exit(0);" in the default case?  Just to check that it's actually being reached.

 

I have a suspicion that IndexPage() is not working as you expect.

here, i'll post my entire code.

 

here is the switch in index.php

 

switch($_GET['p']) 
{
  case 'about':
    AboutPage();
    break;
    
  case 'services':
    ServicesPage();
    break;
    
  case 'scripts':
    ScriptsPage();
    break;
    
  case 'websites':
    WebsitesPage();
    break;
    
  case 'images':
    ImagesPage();
    break;
    
  case 'contact':
    ContactPage();
    break;
    
  default:
    IndexPage();
}

 

here is the menu in header.php (header.php is included at the top of index.php):

 

    <table cellpadding="0" cellspacing="0" border="0" width="1%">
    <tr>
    
    <?php
    
    if (is_null($_GET['p']))
    { 
      echo "<td class=\"topmenu2\"> \n"; 
      echo "HOME \n";
      echo "</td> \n";
    }
    else
    { 
      echo "<td class=\"topmenu\"> \n"; 
      echo "<a href=\"index.php\">HOME</a> \n";
      echo "</td> \n";
    }
    
    if ($_GET['p'] == "about")
    { 
      echo "<td class=\"topmenu2\"> \n"; 
      echo "ABOUT \n";
      echo "</td> \n";
    }
    else
    { 
      echo "<td class=\"topmenu\"> \n"; 
      echo "<a href=\"index.php?p=about\">ABOUT</a> \n";
      echo "</td> \n";
    }
    
    if ($_GET['p'] == "services")
    { 
      echo "<td class=\"topmenu2\"> \n"; 
      echo "SERVICES \n";
      echo "</td> \n";
    }
    else
    { 
      echo "<td class=\"topmenu\"> \n"; 
      echo "<a href=\"index.php?p=services\">SERVICES</a> \n";
      echo "</td> \n";
    }
    
    if ($_GET['p'] == "scripts")
    { 
      echo "<td class=\"topmenu2\"> \n"; 
      echo "SCRIPTS \n";
      echo "</td> \n";
    }
    else
    { 
      echo "<td class=\"topmenu\"> \n"; 
      echo "<a href=\"index.php?p=scripts\">SCRIPTS</a> \n";
      echo "</td> \n";
    }
    
    if ($_GET['p'] == "websites")
    { 
      echo "<td class=\"topmenu2\"> \n"; 
      echo "WEBSITES \n";
      echo "</td> \n";
    }
    else
    { 
      echo "<td class=\"topmenu\"> \n"; 
      echo "<a href=\"index.php?p=websites\">WEBSITES</a> \n";
      echo "</td> \n";
    }
    
    if ($_GET['p'] == "images")
    { 
      echo "<td class=\"topmenu2\"> \n"; 
      echo "IMAGES \n";
      echo "</td> \n";
    }
    else
    { 
      echo "<td class=\"topmenu\"> \n"; 
      echo "<a href=\"index.php?p=images\">IMAGES</a> \n";
      echo "</td> \n";
    }
    
    if ($_GET['p'] == "contact")
    { 
      echo "<td class=\"topmenu2\" style=\"border-right: 0px;\"> \n"; 
      echo "CONTACT \n";
      echo "</td> \n";
    }
    else
    { 
      echo "<td class=\"topmenu\" style=\"border-right: 0px;\"> \n"; 
      echo "<a href=\"index.php?p=contact\">CONTACT</a> \n";
      echo "</td> \n";
    }
    
    ?>
    
    </tr>
    </table>

 

get what im trying to figure out now?

try to echo the get p inside the switch if you cant still figure the error by that post the code having the switch statement

 

I dont get any errors.

 

I want the TD class to be "topmenu2" for the HOME link when someone tries to go to a ?p=page that does not exist.

 

ahh I can't explain it....

Scarhand, can you create a small piece of code that replicates your problem.  Then we can debug that.

 

The other option is to post your FULL code, along with instructions on how to replicate the problem.

 

So far, you have only given us portions of your code, and all those portions work correctly.  Showing us code that works does not help us fix your problem :)

Ok.  Given that description, the simplest way I can think of is to set a variable inside the default case, like this:

 

...
default:
  $default_case = true;
  IndexPage();
}

 

Then later you can do this:

 

if ($default_case) {
  # Do the default case stuff
}

 

Edit:  If you are inside a function, you must do "global $default_case;" before accessing the variable

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.