Current location: Hot Scripts Forums » Programming Languages » PHP » Page dependent include - how?


Page dependent include - how?

Reply
  #1 (permalink)  
Old 08-12-05, 05:42 AM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Question Page dependent include - how?

I want to include a menu item on a particular php page on my site. I'm using an include to display the menu (includes-menu.php) on my pages (index.php, page1.php etc.).

PHP Code:

<?php 

include "../includes-menu.php";
?>
If the user is at page3.php, I want to add an extra menu item. I imagine the code will need to check to see the name of the current page and if it is not page3.php it won't include the additional code, if it is, then it will display the code.

Should the additional code be hard coded inside an "if" statement" or as a further include in the "if" statement? (an include in an include?)

How can I achieve this please?
Reply With Quote
  #2 (permalink)  
Old 08-12-05, 06:36 AM
shadi's Avatar
shadi shadi is offline
Wannabe Coder
 
Join Date: Aug 2004
Location: EGY
Posts: 145
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

 
$Path 
$_SERVER["SCRIPT_NAME"]; // returns the current path such /www/page3.php
 
$path_array explode("/",$Path); // splits the path using ( / ) 
                                         // if the path was /www/page3.php
                                         // after spliting it will be array of 2 elements
                                        // array ('www','page3.php')
 
 
if( in_array("page3.php",$path_array) ) // we will use the in_array() to see if                                      //page3.php is an element of the the array or not
{
    include 
"page3_menu.php";

__________________
Email : write2shadi at gmail dot com
MSN : write2shadi at gmail dot com
Aim : shadiaim7
Skype: shadi_skype
Reply With Quote
  #3 (permalink)  
Old 08-12-05, 10:37 AM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
shadi,

Thanks for your quick reply, I'll give it a go.

If I wanted to specify a number of other pages to also show the extra menu item on, would I do something like this?

PHP Code:

if( in_array("page3.php","page4.php","page4.php",$path_array) ) 

Thanks,

Simon
Reply With Quote
  #4 (permalink)  
Old 08-12-05, 12:32 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
You can just use basename();
PHP Code:

<?php


$filename 
basename($_SERVER['REQUEST_URI'], '.php');

if (
$filename == 'page3')
include 
'page3_menu.php';

?>
Or multiple pages:
PHP Code:

if (eregi('page3|page4|page5'$filename))

include 
'page3_menu.php'

Last edited by Keith; 08-12-05 at 12:37 PM.
Reply With Quote
  #5 (permalink)  
Old 08-13-05, 04:03 AM
liljoeyjordison liljoeyjordison is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Probably the least usefull, but the shortest
<?php
$showExtraStuff = true;

include ("../includes-menu.php");
?>
Then on the other page use
<?php
if ($showExtraStuff == true) {
echo" STUFF " ; } ;
?>
Reply With Quote
  #6 (permalink)  
Old 08-14-05, 01:19 AM
maddog39 maddog39 is offline
Newbie Coder
 
Join Date: May 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
If this is just suppoed to be an auto-updating PHP navigation than heres mine.
PHP Code:

<?php

$page 
= @$_GET['page'];
if ((isset(
$page)) && (file_exists($page ".php"))) {
   include(
$page ".php");
} else {
   echo 
"Page does not exist.";
   exit();
}
?>
__________________
My PHP Development Site: www.madphp.com
Reply With Quote
  #7 (permalink)  
Old 08-17-05, 05:52 AM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for all of the replies!

So many ways to skin the rabbit!

Cheers,
Simon
Reply With Quote
  #8 (permalink)  
Old 08-27-05, 07:22 AM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for all of the tips here guys!

As a further aid to my learning, how would you do it if you wanted to show the include on ALL pages EXCEPT one named page?

This is like a "If something IS NOT true" then do something, rather than a "If something IS true" then do something as the above examples show.

Thanks,
Reply With Quote
  #9 (permalink)  
Old 08-27-05, 04:11 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Reply With Quote
  #10 (permalink)  
Old 08-27-05, 04:20 PM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Great, thanks!

It's a great help when you know where to look for the answers. I've bookmarked the site for extensive further reading!

Cheers,

Simon
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
PHP help... Orchidsdance PHP 8 09-25-04 07:58 PM
page browsing problem mivec PHP 3 04-17-04 03:43 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM
"10 most requested pages" script to include in page avx Script Requests 5 08-06-03 03:26 AM


All times are GMT -5. The time now is 07:01 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.