IPB's page controller - php

Anyone knows how Invision Power Board makes their urls like the following?
index.php?showuser=349
index.php?showtopic=83
index.php?showforum=9
and just pages:
index.php?act=register
index.php?act=about
and so on. How they do it? I'm sure they don't do it like i do now:
if (isset($_GET['showtopic'])){
include('viewtopic.php');
else if (isset($_GET['showuser'])){
include('viewuser.php');
}
else if (isset($_GET['act']) && $_GET['act'] == 'register'){
include('register.php');
}
else if (isset($_GET['act']) && $_GET['act'] == 'about'){
include('about.php');
}
else
{
echo "page not found.";
}

To do the "act=" thing you don't need a huge chain of if statements. You could do it like this, for example:
$pages = array('register', 'about', ...);
if (in_array($_GET['act'], $pages)) {
include $_GET['act'].'.php';
} else {
// display an error
}

Maybe you could add it to an array.
$pages = array('showtopic', 'showuser');
foreach ($pages as $page) {
if (intval($_GET[$page])) {
include("$page.php");
}
}

Related

If statement inside another one is false, return to the else of original if statement

Is there any way in PHP to return at else of first statement, if the second statement which is inside of first, is false
if($first == true) {
//other code which is not necessary to mention here!
if($second == true){
// do smth
}
else{
return to the else of $first statement
}
//other code which is not necessary to mention here!
}
else{
//do smth else
}
Yes, there are multiple ways. For starters, just combine both the statements and give another condition:
if ($first == true && $second == true) {
// do smth
} elseif ($first == true && $second == false) {
// else of$first statement
} else {
//do smth else
}
This can be used as a guidance to get an idea to start. But if you can get a real world example, there can be conditions grouped, tailored to your requirement.
While there is no native way to jump to outer elses from an inner else, but you can set a flag for later processing:
$do_else = false;
if($first == true) {
//other code which is not necessary to mention here!
if($second == true){
// do smth
}
else{
$do_else = true;
}
//other code which is not necessary to mention here!
}
else{
$do_else = true;
//do smth else
}
if($do_else){
//do smth else
}
If the answers above doesn t help you in the real situation, you can create a function for execute in 'else' statements to avoid code duplication

Foreach if and else problems PHP

I am having a few problems with php when I use a foreach() for 10 arrays and use the if(variable = something){}else{} to show what i want from it.
The else returns everything 10x.
What I need help with is when 6111 or 6112 does not exists it shows only once what i have in the else{} instead of showing it 10x since there is 10 arrays.
foreach(){
if($statplayemasteriesslotsID == 6111){
//dothis
} else if($statplayemasteriesslotsID == 6112){
//dothis
} else {
//dothis
}
}
Is there anyway to do this?
I also tried doing this, with the if{} else {} out side the foreach but it instead of returning everything multiple time it mixes them all up in each other.
foreach(){
if($statplayemasteriesslotsID == 6111){
$statplayemasteriesslotsID1 = 6111;
} else if($statplayemasteriesslotsID == 6112){
$statplayemasteriesslotsID1 = 6112;
}
}
if($statplayemasteriesslotsID1 == 6111){
//dothis
} else if($statplayemasteriesslotsID1 == 6112){
//dothis
} else {
//dothis
}
I currently am using the last way i done it if anyone knows away to stop this please let me know. thanks in advance
I'd do this:
$doC = false;
foreach(){
if($statplayemasteriesslotsID == 6111){
//dothis
} else if($statplayemasteriesslotsID == 6112){
//dothis
} else {
//don't do it yet, just set a flag:
$doC = true;
}
}
if($doC) {
// do it once only!
}

How do I add an if / else?

I am using the following code to pass a variable. if variable = a, do nothing.
I then want to check if variable = a, do nothing, if b, do nothing, else do something
<?
if($_GET['pageid'] == 'a'){
} else {
include('header_image.php');
}
?>
Above is the code I have working correctly for one vartiable.
How do I add an if / else?
if($_GET['pageid'] != 'a' && $_GET['pageid'] != 'b'){
//do smth
}
This is a comment - i want the formatting...
To do what you want:
if ($_GET['pageid'] == 'a') {
// do nothing for now
}
elseif ($_GET['pageid'] == 'b') {
// do some more nothing...
}
else { // we do something...
include('header_image.php');
}
You could combine the 'do nothing' tests as:
if ( $_GET['pageid'] == 'a'
|| $_GET['pageid'] == 'b') {
// do nothing for now
}
else { // we do something...
include('header_image.php');
}
I agree it reads better than the 'not equal and' tests. However, that is what 'programmers' use so it is worthwhile getting used to it.

PHP Main page script with GET Won't work

I've made a simple index.php script for my homepage, however, it won't work.
<?php
$Query = $_GET['p']
if (strtolower($Query) == "about") {
include("About.php")
die()
}
if (strtolower($Query) == "") {
include("Home.php")
die()
}
if ($Query) {
include("Home.php")
die()
}
?>
It's my first time coding in php so i don't know what is wrong.
First you need simicolons. Second, why die? Why not do something like.
<?php
$Query = $_GET['p']
if (strtolower($Query) == "about") {
include("About.php");
} else {
include("Home.php");
}
?>

Optimising a PHP If/Else statement

I'm attempting to optimise the following PHP If/Else statement. Could I rewrite the code to make use to case and switch, or should I leave it as it is, or what?
Code:
if(empty($_GET['id'])){
include('pages/home.php');
}elseif ($_GET['id'] === '13') {
include('pages/servicestatus.php');
}elseif(!empty($_GET['id'])){
$rawdata = fetch_article($db->real_escape_string($_GET['id']));
if(!$rawdata){
$title = "";
$meta['keywords'] = "";
$meta['description'] = "";
}else{
$title = stripslashes($rawdata['title']);
$meta['keywords'] = stripslashes($rawdata['htmlkeywords']);
$meta['description'] = stripslashes($rawdata['htmldesc']);
$subs = stripslashes($rawdata['subs']);
$pagecontent = "<article>" . stripslashes($rawdata['content']) . "</article>";
}
include("includes/header.php");
echo $pagecontent;
if(!$rawdata){
error_404();
}
}
Thanks
I hate switch statements, but its personal preference to be honest. As far as further optimization i'd suggest taking a look at some form of assembly language. It will give you some general ideas on how to make conditional statements more efficient. That is, it will give you a different out look on things.
if(!empty($_GET['id']))
{
if($_GET['id'] == '13')
{
include('pages/servicestatus.php');
}
else
{
$rawdata = fetch_article($db->real_escape_string($_GET['id']));
if (!$rawdata) {
$title = "";
$meta['keywords'] = "";
$meta['description'] = "";
} else {
$title = stripslashes($rawdata['title']);
$meta['keywords'] = stripslashes($rawdata['htmlkeywords']);
$meta['description'] = stripslashes($rawdata['htmldesc']);
$subs = stripslashes($rawdata['subs']);
$pagecontent = "<article>" . stripslashes($rawdata['content']) . "</article>";
}
include("includes/header.php");
echo $pagecontent;
if (!$rawdata) {
error_404();
}
}
}
else
{
include('pages/home.php');
}
switch would be appropriate if you had several discrete values for $_GET['id'] that you were checking for.
One suggestion I can make for the sake of readability is that
} elseif (!empty($_GET['id'])) {
only needs to be
} else {
Well i don't think it's necessary to switch to a swith
but you could change
} elseif (!empty($_GET['id'])) {
to just
}else{
You may want to look into breaking up your code into a MVC form; that would make it much easier to maintain your code. At least put the last clause into another file, probably called default.php and include it. Also, you might create an array of id => file key/value sets, lookup the id, and include the file.
if (isset($_GET['id'])) {
$pages = array(
0 => 'home.php',
13 => 'servicestatus.php'
);
if (isset($pages[$_GET['id']])) {
include('pages/' . $pages[$_GET['id']]);
} else {
include('pages/default.php');
}
}
Yes, switch is evaluate once, is efficient than if elseif,
and is easier to maintain with this given structure
switch ($_GET['id'])
{
case 13: ... break;
case 0 : ... break;
default: ... break;
}
I dont know, if you should, or should not, but here I wouldnt. The main reason is, that there is at least one statement, you can omit, and then, you will have just a if-elseif-else-Statement
if (empty($_GET['id'])) { /* code */ }
elseif ($_GET['id'] === '13') { /* code */ }
elseif (!empty($_GET['id'])) { /* code* }
is the same as
if (empty($_GET['id'])) { /* code */ }
elseif ($_GET['id'] === '13') { /* code */ }
else { /* code* }
In the block after that, the statement if(!$rawdata) is also duplicated.

Categories