We currently use Switch case url config to help us with the navigation on some of our urls, Im not sure if there is an easier way to do it but i couldnt seem to find 1.
<?php if (! isset($_GET['step']))
{
include('./step1.php');
} else {
$page = $_GET['step'];
switch($page)
{
case '1':
include('./step1.php');
break;
case '2':
include('./step2.php');
break;
}
}
?>
Now this system works perfectly but the only snag we hit is if they type in xxxxxx.php?step=3 boom they just get a blank page and that should be correct as there is no case for it to handle '3' but what i was wondering is .. is there any php code i could add to the bottom that may tell it for any case other than those 2 to redirect it back to xxxxx.php ?
Thanks
Daniel
Use the default case. That is, change your switch to something like this:
<?php if (! isset($_GET['step']))
{
include('./step1.php');
} else {
$page = $_GET['step'];
switch($page)
{
case '1':
include('./step1.php');
break;
case '2':
include('./step2.php');
break;
default:
// Default action
break;
}
}
?>
The default case will be executed for every case which is not explicitly specified.
All switch statements allow a default case that will fire if no other case does. Something like...
switch ($foo)
{
case 1:
break;
...
default:
header("Location: someOtherUrl");
}
would work. You may, however, want to Google around for other, more robust and extensible, page dispatch solutions.
How about a different approach with something along the lines of:
<?php
$currentStep = $_GET['step'];
$includePage = './step'.$currentStep.'.php'; # Assuming the pages are structured the same, i.e. stepN where N is a number
if(!file_exists($includePage) || !isset($currentStep)){ # If file doesn't exist, then set the default page
$includePage = 'default.php'; # Should reflect the desired default page for steps not matching 1 or 2
}
include($includePage);
?>
Related
so I'm trying to make my own MVC website, I figured the best way to learn PHP better and tackle the big MVC issue is to start a full project. The thing is, I'm stuck at the router, I can't figure out how best to write it, I think this may be a good way at least for the moment but I'm not sure if it is a ..viable one. I was basically thinking of calling the right controller according to the switch case and then if there is a second url param(I've assumed it would be the id of a article for now) to call a method calling a single article and if there isn't a second param to call a method that calls all articles, but I would like to know if this is a bad way of doing it.
function call($controller, $id='') {
switch($controller) {
case '':
break;
case 'pages':
$controller = new PagesController();
break;
case 'articles':
require_once('controllers/' . $controller . 'Controller.php');
require_once('models/articles.php');
$controller = new ArticlesController();
if(!$id){
$controller->{ "blog" }();
}else{
$controller->{ "article" }($id);
}
break;
default:
header("HTTP/1.0 404 Not Found");
include('/views/404.php');
exit;
break;
}
}
P.S. For now I'm only working with the articles case, that's why the first case only breaks without doing anything and such. Thanks in advance.
Sure can, see the example below (this is proof of concept and not tested).
switch ($controller) {
case 'pages':
break
case 'articles':
if ($Apples != $Pears)
{
$Tomatoes="Red";
}
case "52":
case "109":
case "110":
//do nothing and exit from switch
break;
default:
if ($a == $b)
{
$c = "cats!";
}
break;
}
Note that you can also use a switch inside a switch, just like nested if statements.
If you have only 2 or 3 option then you can use if else otherwise use nested switch.
You can use if in switch also switch in another switch.
guys! I am a newbie. I think about it all day but still not much..
I have one index.php file, one switch statement:
$current_page = $_GET['page'];
switch ($current_page) {
case ('homepage'):
include 'contents/homepage.php';
break;
case ('about'):
include 'contents/about.php';
break;
case ('contacts'):
include 'contents/contacts.php';
break;
default:
include 'contents/homepage.php';
}
I include a file if the get attribute is some of the names.
And if the URL is for example: myproject/index.php?page=about
The proper content file is included.
But when the URL is just: myproject/index.php
No file is included.
I was thinkig to add something like:
if (!isset($_GET['page'])) {
header('Location: index.php?page=homepage');
}
But it is kind of ugly I think and the URI cannot be nver just index.php.. And what if the parameter is not "page"... It is a problem again.
Do you have in mind some simple understandable solution for this. How to include the proper contents according to the different pages?
Thank you very much!
First, when checking if an array's key is set, use array_key_exists instead. It is much faster for the parser.
Second, you shouldn't redirect the page. Instead, use a default value, like so:
// Set default value
$current_page = 'homepage';
// Change value if `page` is specified
if(array_key_exists('page',$_GET) {
$current_page = $_GET['page'];
}
// Check page
switch ($current_page) {
case 'about':
include 'contents/about.php';
break;
case 'contacts':
include 'contents/contacts.php';
break;
case 'homepage':
default:
include 'contents/homepage.php';
}
Additionally, as I have done above, you don't need to specify the include for homepage twice. Without a break statement, the parser will go into the next case statement. If you want to treat multiple values the same, you can simply specify them subsequently.
I hope this helps!
$current_page = isset($_GET['page'])?$_GET['page']:'homepage';
This should do it
$current_page = isset($_GET['page']) ? $_GET['page'] : null;
switch ($current_page) {
case ('homepage'):
include 'contents/homepage.php';
break;
case ('about'):
include 'contents/about.php';
break;
case ('contacts'):
include 'contents/contacts.php';
break;
default:
include 'contents/homepage.php';
}
I have an issue with this switch case, I will be glad if someone could give me a hint on what's going wrong.
The lines below keep on adding the case 1, 2 or 3 below the default one (centre_1) instead of replacing it.
I can't find where it goes wrong. Here is the code :
<?php include("mx/centre_1.php");
if(!isset($_GET['page'])) {
$page='centre_mx.php';
} else {
switch($_GET['page']) {
case'centre_1':
include('/mx/centre_1.php');
break;
case'centre_2':
include('/mx/centre_2.php');
break;
case'centre_3':
include('/mx/centre_3.php');
break;
}
}
?>
Any assistance will be helpful.
The reason '/mx/centre_1.php' is always being displayed is because you have it at the top of your code, outside of the if and switch statement. Due to this it'll be included on every page load.
In order to only have '/mx/centre_1.php' appear when no other option is selected you need to make it the default switch case.
$page = '';
if(in_array('page', $_GET)) {
$page = $_GET['page'];
}
switch($page) {
case 'centre_2':
include('/mx/centre_2.php');
break;
case 'centre_3':
include('/mx/centre_3.php');
break;
default:
include('/mx/centre_1.php');
}
The default case in a switch statement will happen when none of the other cases match the variable provided. That is, if $_GET['page'] doesn't equal centre_2 or centre_3 then the default code will be performed.
I'd suggest reading up more on switch statements here, since you don't seem to understand how they work.
PHP script that I'm using contains the switch statement and break to prevent the code from running into the next case automatically:
if (!isset($a)) $a = '';
switch($a)
{
case 1:
default:
// some code
break;
case 2:
// some code
break;
case 3:
// some code
break;
}
How can I prevent users to write in URL as "$a" some number that does not exist in php switch statement?
For example, in this example above, if someone writes as a url indes.php?a=5 should get a message that the link is not correct. What is the best way to do that?
Another thing that interests me, is there any limit on the number of switch statements that it is wise to use on one page or can the size of that page can cause the problem if it is to too large?
Add this to the end of the switch.
default:
echo 'not correct';
break;
From php docs:
A special case is the default case. This case matches anything that wasn't matched by the other cases. For example:
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
http://php.net/manual/en/control-structures.switch.php
Add the default case.
default:
echo 'Invalid Option';
break;
And there is no limit for the cases in switch.
Update:
No matter what ever the size of the page is. But surly it depends on the script or code written inside the cases. It it is time consuming than that will effect.
The placement of your default tag might be causing an issue, but I'm not 100% sure of this:
if (!isset($a)) $a = '';
switch($a)
{
case 1:
default:
// some code
break;
case 2:
// some code
break;
case 3:
// some code
break;
}
The individual case statements execute whenever there is a match with $a. For example if the user submitted 3 (thus $a==3), then case 3 would execute. It will continue to execute until the break; statement is hit. The default block is only executed if no case statements match the value contained in $a.
For example if the user submitted 5 (thus $a==5), there is no case 5: so the default block would be executed. Thusly, it's usually a standard practice to place your default: block at the end of your switch statement as follows to show that if no case statements match the condition, it will be executed last.
if (!isset($a)){
$a = '';
}
switch($a)
{
case 1:
//some code
break;
case 2:
// some code
break;
case 3:
// some code
break;
default:
//code displayed when $a does not match any case statements
}
Hope that helps. Also, switch statements execute quite fast, they are basically similar to nested if statements. Thus there is no limit really, however, code optimization is always something you should strive for.
Your switch statement break because your using wrong structure of switch, check complete switch statement reference here
<?php
$i = 1;
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo 'no case match';
break;
}
?>
im trying to create a website using switch commands to navigate with the contents displayed in a table via echo content. everything works fine. but one of the pages consists of multiple pages. The address looks like this website/index.php?content=home etc.
but i want to make it like this for the page consisting multiple pages website/index.php?content=home&page=1
my index code:
<?php
switch($_GET['content'])
{
case "home":
$content = file_get_contents('home.php');
$pages = file_get_contents('pages/1.php'); //like this?
break;
case "faq":
$content = file_get_contents('faq.php');
break;
default:
$content = file_get_contents('home.php');
break;
}
?>
//some code
<?php echo $content; ?>
//some code
the home php:
<?php
switch($_GET['page'])
{
default:
case "1":
$page = file_get_contents('pages/1.php');
break;
default:
$page = file_get_contents('pages/1.php');
break;
}
?>
// some code
<?php echo $page; ?>
//some code
go to page etc.
but when i do it like this the echo command shows me the code of the home.php but not the one of the page i wanna load inside of it.
i appreciate any kind of help!
"default" must logically always come last in your switch, since it will match any input that hasn't already been matched by a previous "case" statement.
If you want to do certain actions for multiple values, you can do it like this:
switch ( $var ) {
case 'foo':
case 'bar':
// Do something when $var is either 'foo' or 'bar'
break;
default:
// Do something when $var is anything other than 'foo' or 'bar'
break;
}
Try using include() instead.
Also, as #Rob said, your second switch() statement is ill formatted. default: should always be last and is used as a "catch-all" for values you didn't specify earlier.
first of all, why don't you use include() instead of file_get_contents()?
Second of all: you could use your "manager" page in this way:
<?php
$myPage = $_GET['page'];
$myContent = $_GET['content']
switch($myContent){
case "mycontent":
case "home": include('/home.php');
if(!empty($myPage)){
include ('/pages/'.$myPage.'.php');
}
break;
default:
// do whatever you want
}
?>
Be careful of (as in steweb's example) including files based on user input, even with a prepended path — remember that there's the possibility of executing untrusted code. (Imagine if $_GET['page'] is set to ../../../some-evil-file.php, for example.)
To avoid this, it's easiest to use a whitelist:
$pages = array( 1, 2, 18, 88 );
switch ( $_GET['content'] ) {
case 'home':
if ( in_array( $_GET['page'], $pages ) ) {
include 'pages/' . $_GET['page'] . '.php';
} else {
// If an invalid page is given, or no page is
// given at all, include a default page.
include 'pages/1.php';
}
break;
}