View Full Version : Resolved switch statement with no page found option
?foru
02-28-2014, 02:44 AM
All,
I have been running this for quite some time and it's solid, but I want to be able to utilize a page not found option.
<?php
switch($_GET['page']){
case "contact":
$displaypage = "page_content/contact.php";
$title = "Contact";
$description = "";
$keywords = "";
break;
case "about":
$displaypage = "page_content/forum.php";
$title = "About";
$description = "";
$keywords = "";
break;
default:
$displaypage = "page_content/home.php";
$title = "Home";
$description = "";
$keywords = "";
break;
}
?>
I want users to be able to get a "page not found" message or set in place a 404.php page if they type domain.com/index.php?page=abc or they misspell the page name like "cntact"
This switch statement can get pretty long obviously if there are many pages, so maybe there is a better way to write this with say an array like...
$page = array('home', 'about', 'contact');
$titles = array('Home', 'About us', 'Contact Us');
Thank you
I want users to be able to get a "page not found" message or set in place a 404.php page if they type domain.com/index.php?page=abc or they misspell the page name like "cntact"
The logical place for a "page not found" option would be in the default case.
This switch statement can get pretty long obviously if there are many pages, so maybe there is a better way to write this with say an array…
Sure. I'd highly recommend making it a multi-dimensional array, however, not separate arrays for the title, page name, etc.. If you have to make sure items are always in a particular (but arbitrary) order, it is too easy to mess things up. Maybe like:
<?php
$pages = array(
"home" => array(
"title" => "Home",
"display" => "page_content/home.php",
"description" => "This is the home page!",
),
"about" => array( /* etc. ... */ ),
// etc. ...
404 => array(
"title" => "Page Not Found",
"display" => "page_content/404.php",
"description" => "The page you requested was not found!",
)
);Then, you could dispose of the switch statement for a simple else/if:
<?php
if( isset( $_GET["page"] ) && isset( $pages[ $_GET["page"] ] ) ){
$page = $_GET["page"];
}else{
$page = 404;
}
$title = $pages[$page]["title"];
$display = $pages[$page]["display"];
$description = $pages[$page]["description"];
?foru
03-05-2014, 02:15 AM
Thank you. Sorry for the delayed reply, that worked! The only thing left I need to do is set a default "home" page if no "page" is specified.
IE. domain.com/ or domain.com/index.php should by default display "page_content/home.php" since the user didn't attempt to reach a page domain.
I've been trying to work with it, and can't seem to get it.
So, you want $page to default to "home" if it is not specified, correct?
Try describing what you want in "psuedo"-code terms:
did user request a page?
yes: does the requested page exist?
yes: show it
no: show a 404 page
no: show the home page
Once you have your plan, it's very easy to translate it into actual code:
<?php
// did user request a page?
if( isset( $_GET["page"] ) ){
// yes: does the requested page exist?
if( isset( $pages[ $_GET["page"] ] ) ){
// yes: show it
$page = $_GET["page"];
else{
// no: show a 404 page
$page = 404;
}
}else{
// no: show the home page
$page = "home";
}
?foru
03-06-2014, 02:49 AM
Thank you for helping to think out the code which helped to visualize it as code. I was getting ELSE parse errors, but after some thought I was able to set the default with the following. I appreciate your help!
if( !isset( $_GET["page"] ) && !isset( $pages[ $_GET["page"] ] ) ){
$page = home;
}
Thank you for helping to think out the code which helped to visualize it as code. I was getting ELSE parse errors, but after some thought I was able to set the default with the following. I appreciate your help!
if( !isset( $_GET["page"] ) && !isset( $pages[ $_GET["page"] ] ) )
You're welcome. Think about that code for a minute, though: If $_GET['page'] does not exist, then is it even possible for it to be an index in your $pages array?
Checking both conditions doesn't make sense:
If the first condition is true, the second is always true also.
If the first condition is false, then it doesn't matter if the second condition is true or not (in fact, PHP doesn't even bother checking).
If you share the code that was giving you a parse error, we can probably figure out why and fix it.
?foru
03-08-2014, 02:33 PM
You're right, and your explanation makes since...thank you! I was able to track down the parse error in the code posted in #4, and the error was throwing due to a missing } prior to the first else. It stated line 27...so I looked above and below that line and caught the error.
// did user request a page?
if( isset( $_GET["page"] ) ){
// yes: does the requested page exist?
if( isset( $pages[ $_GET["page"] ] ) ){
// yes: show it
$page = $_GET["page"];
//added }
}else{
// no: show a 404 page
$page = 404;
}
}else{
// no: show the home page
$page = "home";
}
If it helps anyone else, here is the finalized code.
<?php
$pages = array(
"home" => array(
"title" => "Home",
"display" => "page_content/home.php",
"description" => "This is the home page!",
),
"administration" => array(
"title" => "Administration",
"display" => "page_content/administration.php",
"description" => "This is the administration page!",
),
"404" => array( /* etc. ... */ ),
// etc. ...
404 => array(
"title" => "Page Not Found",
"display" => "page_content/404.php",
"description" => "The page you requested was not found!",
)
);
// did user request a page?
if( isset( $_GET["page"] ) ){
// yes: does the requested page exist?
if( isset( $pages[ $_GET["page"] ] ) ){
// yes: show it
$page = $_GET["page"];
}else{
// no: show a 404 page
$page = 404;
}
}else{
// no: show the home page
$page = "home";
}
$title = $pages[$page]["title"];
$display = $pages[$page]["display"];
$description = $pages[$page]["description"];
?>
You're right, and your explanation makes since...thank you! I was able to track down the parse error in the code posted in #4, and the error was throwing due to a missing } prior to the first else. It stated line 27...so I looked above and below that line and caught the error.
Great, glad you got it working!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.