You are here: Re: 'nested conditional' that can identify parent page? « All PHP « IT news, forums, messages
Re: 'nested conditional' that can identify parent page?

Posted by shimmyshack on 05/14/07 01:42

On May 14, 2:04 am, Alan Jones <a...@jalanjones.com> wrote:
> On 13 May 2007 04:41:19 -0700, shimmyshack <matt.fa...@gmail.com>
> wrote:
>
>
>
> >On May 13, 3:44 am, Alan Jones <a...@jalanjones.com> wrote:
> >> On 12 May 2007 19:20:20 -0700, shimmyshack <matt.fa...@gmail.com>
> >> wrote:
>
> >> >> Thank you very much for the help. :) I'll give your recommendation
> >> >> a run thru, but is there a way to make basename, or a similar
> >> >> function, simply return the filename of the parent page; the page
> >> >> the include is in? Thanks again, I really appreciate any help I can
> >> >> get.
>
> >> >basename($_SERVER['SCRIPT_FILENAME']);
> >> >does exactly that, try it.
>
> >> It returns the filename of the file it is in. I ran...
>
> >> echo basename($_SERVER['SCRIPT_FILENAME']);
>
> >> ...from within the 'include' file index_body.php and it returned
> >> that same filename.
>
> >> I need the code in the include file to somehow determine, or derive,
> >> the name of its 'parent' file in a given instance. This procedure
> >> would be happening within the include file as it resides in the
> >> parent file.
>
> >> Again, thanks for racking your brain on this with me. I'm at a total
> >> loss... :(
>
> >That wasnt quite what I expected you to do, I expected you to place
> >this:
> >echo basename($_SERVER['SCRIPT_FILENAME']);
> >inside the included file and run the website as you would normally, if
> >the included file was indeed included, then you will see a value
> >different from that of the included name.
>
> >I am not sure what you are asking, but I think you are saying
> >1: "I have files a,b,c... each of which include u and I want to
> >include e(signature) inside u only when the "top" file is b"
> >but you could be saying
> >2: "I have files a,b,c... each of which include u,v,w and I want to
> >include e(signature) only inside v" - in which case there is an
> >equally simple answer. just let me know which question you are asking
> >cos I keep answering 1 and were not getting anywhere! :)
>
> >Ultimately there are many ways to skin a cat, but this one just seemed
> >the easiest.
>
> >To see what the value of the variables are do this:
> >(this will let you convince yourself that you /can/ use this method)
>
> >create a file called 1.php, and inside put this:
> ><?php
> >echo '<pre>';
> >echo 'i am file called: ' . __FILE__ . "\n";
> >echo 'request uri: ' . $_SERVER["REQUEST_URI"] . "\n";
> >echo 'script name: ' . $_SERVER["SCRIPT_NAME"] . "\n";
> >echo 'php self: ' . $_SERVER["PHP_SELF"] . "\n";
> >echo 'script filename: ' . $_SERVER["SCRIPT_FILENAME"] . "\n";
> >echo "\ni am going to include the next file\n";
> >include ( '2.php' );
> >?>
>
> >create a file called 2.php and inside put this:
> ><?php
> >#echo '<pre>';
> >echo 'i am file called: ' . __FILE__ . "\n";
> >echo 'request uri: ' . $_SERVER["REQUEST_URI"] . "\n";
> >echo 'script name: ' . $_SERVER["SCRIPT_NAME"] . "\n";
> >echo 'php self: ' . $_SERVER["PHP_SELF"] . "\n";
> >echo 'script filename: ' . $_SERVER["SCRIPT_FILENAME"] . "\n";
> >echo "\ni am going to include the next file\n";
> >include ( '3.php' );
> >?>
>
> >create a file called 3.php and inside put this:
> ><?php
> >#echo '<pre>';
> >echo 'i am file called: ' . __FILE__ . "\n";
> >echo 'request uri: ' . $_SERVER["REQUEST_URI"] . "\n";
> >echo 'script name: ' . $_SERVER["SCRIPT_NAME"] . "\n";
> >echo 'php self: ' . $_SERVER["PHP_SELF"] . "\n";
> >echo 'script filename: ' . $_SERVER["SCRIPT_FILENAME"] . "\n";
> >echo "\ni wont include any more files, see how despite the name of the
> >file changing the others dont, this is because each file is contained
> >within the first.\n";
> >?>
>
> >save them to the same php enabled web accessible folder and in your
> >browser call up 1.php
> >when you view the results you will see what you should have seen the
> >first time.
>
> >Here's the solution using the above method but writing it at the top
> >in index.php (we are still taling about question 1 here).
> >1.php
> ><?php
> >$myname = basename(__FILE__);
> >include('2.php');
> >?>
>
> >2.php
> ><?php
> >if($myname=='index.php')
> >{
> > include_once('signature.php');
> >}
> >include('3.php');
> >?>
>
> >or if you want it to appear in an even more nested include so your
> >head will explode when you come to edit this website in the future
> >3.php
> ><?php
> >if($myname=='index.php')
> >{
> > include('signature.php');
> >}
> >?>
>
> >and so on,
> >the basic concept is that you need logic somewhere to compare the
> >value of the script filename with the value you want, and either
> >have the logic just before the include telling it to be include when
> >script_filename = index.php
> >have the logic in the "top" script, and set a variable which ripples
> >through includes and test for it.
>
> All of your effort is very appreciated, Matt. I'm grateful for the
> time you have put into this, but I think I have figured it out.
>
> Apparently, $_SERVER does not like the use of a http web file path
> when doing an include. I had been usinghttp://jalanjones.com/...
> to specify the path to the file. For the heck of it, just to try
> anything, I changed the path to the local file system syntax,
> 'includes/index_ body.php', and it now works as it should.
>
> I'm using...
>
> if ($_SERVER['PHP_SELF'] == '/index.php'){include('signature.php');}
>
> ...without any errors. If I change 'index' to anything else, for
> example 'index_2', it does not include the signature or give an
> error. The short script is page specific and can be used, with
> modified paths, in any file. That's just my speed; keep it simple
> for stupid. :) I have a lot of learning to do and you helped me get
> over this hump, thank you.

if you will need signature included for a set of files you know in
advance you could do this
(this example works for all index.php filenames because it uses
basename, if you need/want to specify filenames by their paths then
remove the basename part, and specify the files in the array including
their paths
$arrFileNamesWhereSignatureShouldAppear = array(
'index.php',
'contact.php'
);
if( in_array(basename($_SERVER['PHP_SELF']),
$arrFileNamesWhereSignatureShouldAppear) )
{
include('signature.php');
}

remember that this is NOT the simple way. All this might appear
simple, but the rub comes later when you relise you have to rewrite
everything to do something that would have been simple if you had
committed to the learning curve earlier.
You are encouraged to look at best practise examples and follow along,
before thinking you can rewrite the book with "5 minute home brew
architecture" trust me I learned this way too!

for instance here's a better way to include stuff. (still old style
non Object Oriented)

mainpage.php
<?php
#find page you are on from request uri
include('functions.php');
$arrayPageParts = get_page_info( $_SERVER['REQUEST_URI'] );
?>
<?php
#css just for this page
echo $arrayPageParts['css'];
'js just for this page
echo $arrayPageParts['js'];
?>
styles/js for everypage go here
<html><head>
<title><?php echo $arrayPageParts['title']; ?></title>
<?php echo output_meta_tag( $arrayPageParts['description']; ?>
<meta tags: language, description, keywords, etc...
<!--some comment-->
</head><body>
<?php
#this ['body'] could instead be
['menu']
more html...
['content']
etc.. so that more html is in the template, and content elsewhere
try to make as little php and html mix as possible, have a template
which is all the html you need, and fill with pure content
echo $arrayPageParts['body']; ?>
<?php echo $arrayPageParts['signature'];
#which of course is not there if the $page leads to this being null
?></body></html>

I mean this is by no means the right way of coding, but it is a very
simple "template"

the idea of includes can be useful but not as usefule as functions
which get the data and return it into an array

Once again, this might be arguably better, its no OO hMVC pattern but
hey it's better than writing condition includes in the main page, keep
on abstracting away your code into functions and return a array of
paragraphs, menu content etc.. free of html, and use while loops and
so on..
this way if you redesign the site, you just redesign the template, and
the content can come from a db or flat files or another website, you
dont care, and that makes it easy to add pages...

anyway matt out.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация