Page 1 of 1

How Far With FUNCTION: INCLUDE?

Posted: Sun Nov 27, 2005 4:21 am
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]

Posted: Sun Nov 27, 2005 6:03 am
by onion2k
You should definitely replace it with a switch..case statement rather than a bunch of if..else blocks.

Posted: Sun Nov 27, 2005 7:23 am
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.

Posted: Sun Nov 27, 2005 11:41 am
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().

Posted: Sun Nov 27, 2005 12:44 pm
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');
}

?>

Posted: Mon Nov 28, 2005 11:57 am
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

Posted: Tue Nov 29, 2005 8:36 am
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

Posted: Tue Nov 29, 2005 8:38 am
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

Posted: Tue Nov 29, 2005 8:44 am
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]