Forum Moderators: coopster

Message Too Old, No Replies

Multiple includes on one click

         

RuneRifle

11:57 am on Apr 12, 2010 (gmt 0)

10+ Year Member



Hello all!

i'm new to php and I really want to know it, but I really don't know how to do this one:

I'm making a website with includes, and got this include script for my content:

<?php
$path = 'includes/';
$default_page = 'home';

$page = isset( $_GET['page'] ) ? $_GET['page'] : $default_page;
$notallowed = array( '.', '\\', '/' );

if( file_exists( $path . $page . '.php' ) and !in_array( substr( $page, 1, 1 ), $notallowed ) and strpos( $page, '../' ) === false and strpos( $page, '..\\' ) === false )
{
include( $path . $page . '.php' );
}
else
{
include( $path . $default_page . '.php' );
}
?>


Now this works perfectly, but I want to use a second php include script for my menu.
My menu will be accessed just like the content part, and with the same link, because i have a menu and a submenu. The submenu should change just like the content...

Is this possible? and if yes, how to do it?

Matthew1980

12:39 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there RuneRifle,

Welcome to the forum!

What you are doing seems a bit over the top to me, but if it makes you happy!

Just one ajdustment needed methinks, as I do more or less the same thing, the joy of ternary op's, they are you friend ;-p

$page = (isset($_GET['page']) ? strip_tags($_GET['page']) : 'home');

I have just placed strip_tags() around the $_GET so that incoming data is cleaner, and also there is no need to refer to the 'default' var, so I placed it in as the return false if not set :)

Also for returning the current dir try this:-

define('BASE_PATH', dirname(__FILE__). "/"); //current dir..

then just refer to the constant everytime you need a file from the root outwards ie: include(BASE_PATH."contents/about.php");

With regards to menu includes, I dont quite follow what you are trying to achieve here, is the structure of the HTML (css or table drive) intact, when you are doing the includes or is this at a cellular level?

Don't forget you can do an include in an include within an include ;-p

I hope that makes sense, but I am intrigued as to how you are trying to get this structured,

Cheers,
MRb

RuneRifle

12:58 pm on Apr 12, 2010 (gmt 0)

10+ Year Member



maybe it is over the top (i dont know lol) but here is the layout and how i want it, with the menu, submenu, and content:
<snip>

so when that link in the menu is clicked, the submenu and content must change to a specific page.

I'm still really newb with php, so please explain it well :P

[edited by: eelixduppy at 2:47 pm (utc) on Apr 12, 2010]
[edit reason] no personal URLs, please [/edit]

Matthew1980

1:55 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there RuneRifle,

The Mods will nuke that link if its a personal site, just thought I would point you to the TOC's at the top there, a guide to whats tollerated on here ;)

The easiest way to go about this would be a switch statement IMO, you are using the value of $_GET to determine what is shown, so I would think that you have some generic content header.php, footer.php and morethan likely content.php for the content ?

If thats something like it you could include the menu dependant on how the $_GET is set from within a secondary include.

<?php

switch(strip_tags($_GET['page']))
{
case "home":
//your includes here
break;

case "contact":
//your includes here
break;

case "about me":
//your includes here
break;
}

?>

Have a file called something like nav.php and in there you can have an if clause to say something like this:-

if(isset($_GET['page']) && ($_GET['page'] == "home"))
{
//either include nav menu here or build it here for the homepage
}else
{
//either include nav menu here or build it here for the other pages
}

Of course this assumes as you have only two menus to use. Alternatively use an elseif for different options.

if(try this)
{
// do this
}
elseif(try this instead)
{
//do this for that
}
elseif(no try this)
{
//do this then
}

I hope you get this sort of structure from that example.

Cheers,
MRb

Readie

2:09 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Probably better to use

if() {

} elseif() {

} elseif() {

} else {

}

Instead of a switch statement if you're using $_GET to control what page the user is seeing. That way you can 404 them with very little difficulty if they enter something we don't want.

Matthew1980

2:40 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Readie,

Thats what this takes care of:-

$page = (isset($_GET['page']) ? strip_tags($_GET['page']) : 'home');

That's why I don't put a default: clause in the switch :)

I think functionality wise that there is a great deal of similarity between a switch/if-elseif, I could be wrong, but I do either when the context of a situation asks..

Cheers,
MRb

Readie

4:08 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You misunderstand me Matt :)

With a switch statement, what happens when none of the expected values are provided?

the else { clause allows us to throw an error page.

$page = (isset($_GET['page']) ? strip_tags($_GET['page']) : 'home');

Just prevents the visitor causing damage, and provides a default value.

rocknbil

11:03 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what happens when none of the expected values are provided?


switch(strip_tags($_GET['page'])) {
case "home":
//your includes here
break;
// etc
default:
// default action

}

:-)

Readie

11:44 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



switch(strip_tags($_GET['page'])) {
case "home":
//your includes here
break;
// etc
[b]default:
// default action[/b]
}


:-)

Ahh. Thanks :) I havn't really explored switch statements all that much.

I must ask, is a switch statement more efficient than an if/elseif/else statement?

Matthew1980

7:15 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie, & Correct me if I am wrong Rocknbil :)

Switch statements are dependent on the content of the var given to it, you can groupe them (multiple case):-

switch($var)
{
case "first_case":
case "Second_case":
case "Third_case":
//code here if you have 3 instances that will/can be treated similar
break;
}

I use the above for my custom thumbnailer:-

switch($mime_type)
{
case "JPEG":
case "jpeg":
case "jpg":
case "JPG":
//code here if you have 3 instances that will/can be treated similar
break;
}

Hope you can see the point there,

if/else if

These are meant to provide a means of comparision ie:
(pinched example from php.net )
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}

But you can structure them similar, One thing I find is that you don't need to use exit; in a switch, whereas you do in an if/else situation.

This is my useage/understanding of this anyway, though I am open to suggestion -p

In answer to your question Readie, I think as it is a matter of preference, I use either depending on the context that the data comes in, and how I want to handle it ;-p

Cheers,
MRb

Readie

11:32 am on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aye, I read the php.net :) if it's even the tiniest bit more efficient one way or another it's worth knowing though.

Shall have to check some load times at one point or another when I get home.