How Far With FUNCTION: INCLUDE?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

How Far With FUNCTION: INCLUDE?

Post by poeta_eletrico »

Weirdan | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi Guys & Girls !


I am developing a little project for my own and I am using the same page to output every link within my site. So, when the user hits any link available, I have a $_GET and parse the correct path with, as Ex.: "if ($_GET["x"]){ include ("./path/toincluldepage.php");}elseif... "

I am not sure with but I am perceiving a strange behavior in the status bar of my FIREFOX, when I execute any of these links. I mean: it geting a little much longer to output the requested content. I must inform that, every page has DB access. So I ask you: is there a better way to reduce this or that strange behavior does not have any relation?

That s the condition used to parse any link in the index.php:

Code: Select all

if($_GET['add_mst'] || $_POST['add_mst']){

			include("./inc/inc_add_mst.php");

		}elseif($_GET['add_rsl'] || $_POST['add_rsl']){		

			include("./inc/inc_add_rsl.php");
			
		}elseif($_GET['add_slp'] || $_POST['add_slp']){		

			include("./inc/inc_add_slp.php");

		}elseif($_GET['app_crd_mst'] || $_POST['app_crd_mst']){

			include("./inc/inc_app_crd_mst.php");

		}elseif($_GET['app_crd_rsl'] || $_POST['app_crd_rsl']){

			include("./inc/inc_app_crd_rsl.php");

		}elseif($_GET['app_crd_slp'] || $_POST['app_crd_slp']){

			include("./inc/inc_app_crd_slp.php");

		}elseif($_GET['crd_app_mst'] || $_POST['crd_app_mst']){

			include("./inc/inc_crd_applied_mst.php");
		
		}elseif($_GET['crd_app_rsl'] || $_POST['crd_app_rsl']){

			include("./inc/inc_crd_applied_rsl.php");

		}elseif($_GET['crd_app_slp'] || $_POST['crd_app_slp']){

			include("./inc/inc_crd_applied_slp.php");

		}elseif($_GET['balance'] || $_POST['balance']){

			include("./inc/inc_balance.php");

		}elseif($_GET['pending'] || $_POST['pending']){

			include("./inc/inc_pending_cards.php");

		}elseif($_GET['lst_mst']){

			include("./inc/inc_list_mst.php");

		}elseif($_GET['lst_rsl']){

			include("./inc/inc_list_rsl.php");

		}elseif($_GET['lst_slp']){

			include("./inc/inc_list_slp.php");

		}elseif($_GET['prod']){

			include("./inc/inc_products.php");

		}else{ default content - no include for that }
I imagined use instead classes ... but I am not sure...

I really appreciate any feedback for that question.

Poeta_Eletrico


Weirdan | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You should definitely replace it with a switch..case statement rather than a bunch of if..else blocks.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

poeta_eletrico wrote:I am developing a little project for my own and I am using the same page to output every link within my site. So, when the user hits any link available, I have a $_GET and parse the correct path with, as Ex.: "if ($_GET["x"]){ include ("./path/toincluldepage.php");}elseif... "
That looks like a Front Controller.
Martin Fowler in PoEAA wrote:A Front Controller handles all calls for a Web site, and is usually structured in two parts: a Web handler and a command hierarchy. The Web handler is the object that actually receives post or get requests from the Web server. It pulls just enough information from the URL and the request to decide what kind of action to initiate and then delegates to a command to carry out the action (see Figure 14.2).
poeta_eletrico wrote:I am not sure with but I am perceiving a strange behavior in the status bar of my FIREFOX, when I execute any of these links. I mean: it geting a little much longer to output the requested content. I must inform that, every page has DB access. So I ask you: is there a better way to reduce this or that strange behavior does not have any relation?
If it seems slow to you maybe you should use an opcode cache to speed things up? Just kidding ... though of course that is an option. Getting rid of those if/else statements would help as suggested by Onion2k. Another option is eliminating the Front Controller and use Page Controllers instead.
PoEAA by Martin Fowler wrote:Page Controller:

An object that handles a request for a specific page or action on a Web site.

Most people's basic Web experience is with static HTML pages. When you request static HTML you pass to the Web server the name and path for a HTML document stored on it. The key notion is that each page on the Web site is a separate document on the server. With dynamic pages things can get much more interesting since there's a much more complex relationship between path names and the file that responds. However, the approach of one path leading to one file that handles the request is a simple model to understand.

As a result, Page Controller has one input controller for each logical page of the Web site. That controller may be the page itself, as it often is in server page environments, or it may be a separate object that corresponds to that page.
You would have to reorganize your program into pages however eliminating the if/then statements looks like a lot of work too.
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

Here's how I would code it. Instead of having query strings like

?add_mst=1
?add_rsl=1

I would use a single name

?page=add_mst
?page=add_rst

then in the script

Code: Select all

$page = $_REQUEST['page'];
// check that $page is in an allowed list of pages
if (pageallowed($page)) {
  include("inc/inc_{$page}.php");
} else {
  // default content
}
I don't know if this is any faster. It'll depend on how the pageallowed() function is coded.
I've would do it defining an array that lists all the allowed pages then calling in_array().
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

A page id and the file paths stored in an array is the better practice imo:

Code: Select all

<?php

$pages = array('main', 'register', 'email', 'contact');
$pid = intval($_GET['pid']);

if (array_key_exists($pid, $pages)) {
    include($pages[$pid] . '.inc');
} else {
    include('default.inc');
}

?>
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

onion2k wrote:You should definitely replace it with a switch..case statement rather than a bunch of if..else blocks.
Hi !

Sorry for the delay responding your post. Well, I imagine now that SWITCH would be faster than IF statement but I will consider what SHEILA respond as well. I will reorder things here and let u know !!

Thanks in advance!

Poeta_Eletrico
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

sheila wrote:Here's how I would code it. Instead of having query strings like

?add_mst=1
?add_rsl=1

I would use a single name

?page=add_mst
?page=add_rst

then in the script

Code: Select all

$page = $_REQUEST['page'];
// check that $page is in an allowed list of pages
if (pageallowed($page)) {
  include("inc/inc_{$page}.php");
} else {
  // default content
}
I don't know if this is any faster. It'll depend on how the pageallowed() function is coded.
I've would do it defining an array that lists all the allowed pages then calling in_array().
I take for reference your piece of code and mixed up with the one parsed by Jenk.

Thank you very much for your attention and this really help me a lot !

It really is something pretty simple, but, only when you have an wide view u can see the point to view :-)

Poeta_Eletrico
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

Jenk wrote:A page id and the file paths stored in an array is the better practice imo:

Code: Select all

<?php

$pages = array('main', 'register', 'email', 'contact');
$pid = intval($_GET['pid']);

if (array_key_exists($pid, $pages)) {
    include($pages[$pid] . '.inc');
} else {
    include('default.inc');
}

?>
Hi Jenk,


I did your code but changed just the first statement when u say:

Code: Select all

$pid = intval($_GET['pid']);
To:

Code: Select all

if($_POST['pid']){
	$pid = intval($_POST['pid']);
}elseif($_GET['pid']  || !$_GET['pid']){
	$pid = intval($_GET['pid']);
}
Because there s moment within the pages included, I parse part of my code by POST. The rest is the same way u wrote here !

Thank you very much!!!

Poeta_Eletrico
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if(!empty($_POST['pid'])){
   $pid = intval($_POST['pid']);
}elseif(!empty($_GET['pid'])){
   $pid = intval($_GET['pid']);
}
else {
   $pid = 0;
}
Makes more sense ;)
[/quote]
Post Reply