Jump to content

[SOLVED] help: switch statement


wrathican

Recommended Posts

hi i recently tried using the switch method for a navigation system as opposed to using the get method.

this is the code i am using with the get method commented out:

<?php // Opening tag for PHP

/*$p = $_GET['p'];
if ( !empty($p) && file_exists('./pages/' . $p . '.php') && stristr( $p, '.' ) == False ) 
{
   $file = './pages/' . $p . '.php';
}
else
{
   $file = './pages/default.php';
}

include $file;*/

switch(£_GET['p']){
case "contact":
//sets the variable to contact
$file = './pages/contact.php';
break;

case "portfolio"
$file = './pages/portfolio.php';
break;


}

include $file;

?>

 

this is the code for my index file.

if the url is blank there is no file to include. however i do already have a file that i want to include.

how would i check to see if the url is blank using switch so i can include this file?

 

 

and also. when using switch do i still put ?=variable as the link for links?

and is it supposed to seen in the url like: www.domain.com/index.php?=variable  ?????

Link to comment
https://forums.phpfreaks.com/topic/50817-solved-help-switch-statement/
Share on other sites

switch(£_GET['p']){
  case "contact":
    //sets the variable to contact
    $file = './pages/contact.php';
    break;
  case "portfolio"
    $file = './pages/portfolio.php';
    break;
  default:
    $file = "./pages/yourdefault.php";
    break;
}

include $file;

?>

In a switch statement, after all your case statements, just set up a default at the end to catch anything besides the cases you have declared:

<?php
switch ($_GET['pg']) {
case 'contact':
	require('contact.php');
	break;

case 'about':
	require('about.php');
	break;

default:
	require('default.php');
}
?>

 

Hope this helps.

Hey dude

 

I tend to use this

 

 

$view = $_GET['view'];

// Navigation

if (!empty($view) && file_exists("./" . $view . ".php"))
{
$file = "./" . $view . ".php";
}
elseif (empty($view))
{
$file = "main.php";
}
else
{
$file = "not_exist.php";
}

include $file;

 

I think this would do a similar function to what you are after, plus if the page doesnt exist it redirects to a custom 404 page.

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.