![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| PHP PHP for some can be one of the hardest website programming codes, so do you need help on your PHP script, if it is php4, php5 or lower this is the place for you for any PHP help. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| Hello everyone, any help would be greatly appreciated. What I'm trying to do may not be advisable, but here goes... I want a page named signature.php to appear conditionally as an include within another include so that it will, for example, appear in index.php but not in other result pages that use the same top level include. The method would need to determine what page it is inside of during each given instance. I guess something like... if page is index.php then include file else do nothing A 'nested conditional' seems obvious but I don't know how to create an argument that checks the result page file name or otherwise id's that parent page. Obviously, I'm new to PHP and my understanding of basic programming is very limited. I'm also new to the group. I hope to learn quickly, and I look forward to helping others in the future. | |||
|
| Advertisements |
| | #2 | ||
| On May 11, 11:40 pm, Alan Jones <a...@jalanjones.com> wrote: > Hello everyone, any help would be greatly appreciated. > > What I'm trying to do may not be advisable, but here goes... > > I want a page named signature.php to appear conditionally as > an include within another include so that it will, for example, > appear in index.php but not in other result pages that use the > same top level include. > > The method would need to determine what page it is inside of > during each given instance. I guess something like... > > if page is index.php then include file else do nothing > > A 'nested conditional' seems obvious but I don't know how to > create an argument that checks the result page file name or > otherwise id's that parent page. > > Obviously, I'm new to PHP and my understanding of basic > programming is very limited. I'm also new to the group. I hope > to learn quickly, and I look forward to helping others in the > future. make three php pages, each containing the following apart from the last line <?php echo '<pre>'; echo "me: " . __FILE__; echo $_SERVER["REQUEST_URI"] . "\n"; echo $_SERVER["SCRIPT_NAME"] . "\n"; echo $_SERVER["PHP_SELF"] . "\n"; echo $_SERVER["SCRIPT_FILENAME"] . "\n"; echo "\n\n"; include ( '2.php' ); ?> inside 2.php the last line should be 3.php and inside 3.php theres no include you will see that __FILE__ always contains the name of the script it is in, whether than is included in something else or not. whereas $_SERVER['SCRIPT_FILENAME'] doesnt change, it returns 1.php because that is the script your are executing, so you can use it to find the name of the file that includes the others. your basic script would be if( basename($_SERVER['SCRIPT_FILENAME'])=='index.php' ) { include( 'signature.php' ); } however if you change your server setup this might not always work, as you said before hard coding things like this is generally not a good idea. Instead look at the architecture you are building and see if you cant handle the whole thing in one function somewhere centrally located say in /private/appname/functions this way your life is easier later on. hope that helps. | |||
|
| | #3 | ||
| On 12 May 2007 08:48:01 -0700, shimmyshack <matt.farey@gmail.com> wrote: >On May 11, 11:40 pm, Alan Jones <a...@jalanjones.com> wrote: >> Hello everyone, any help would be greatly appreciated. >> >> What I'm trying to do may not be advisable, but here goes... >> >> I want a page named signature.php to appear conditionally as >> an include within another include so that it will, for example, >> appear in index.php but not in other result pages that use the >> same top level include. >> >> The method would need to determine what page it is inside of >> during each given instance. I guess something like... >> >> if page is index.php then include file else do nothing >> >> A 'nested conditional' seems obvious but I don't know how to >> create an argument that checks the result page file name or >> otherwise id's that parent page. >> >> Obviously, I'm new to PHP and my understanding of basic >> programming is very limited. I'm also new to the group. I hope >> to learn quickly, and I look forward to helping others in the >> future. > >make three php pages, >each containing the following >apart from the last line ><?php >echo '<pre>'; >echo "me: " . __FILE__; >echo $_SERVER["REQUEST_URI"] . "\n"; >echo $_SERVER["SCRIPT_NAME"] . "\n"; >echo $_SERVER["PHP_SELF"] . "\n"; >echo $_SERVER["SCRIPT_FILENAME"] . "\n"; >echo "\n\n"; >include ( '2.php' ); >?> >inside 2.php the last line should be 3.php >and inside 3.php theres no include > >you will see that __FILE__ always contains the name of the script it >is in, whether than is included in something else or not. >whereas $_SERVER['SCRIPT_FILENAME'] doesnt change, it returns 1.php >because that is the script your are executing, so you can use it to >find the name of the file that includes the others. >your basic script would be >if( basename($_SERVER['SCRIPT_FILENAME'])=='index.php' ) >{ > include( 'signature.php' ); >} >however if you change your server setup this might not always work, as >you said before hard coding things like this is generally not a good >idea. Instead look at the architecture you are building and see if you >cant handle the whole thing in one function somewhere centrally >located say in /private/appname/functions >this way your life is easier later on. >hope that helps. Thank you very much for the help. 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. | |||
|
| | #4 | ||
| On 13 May, 00:31, Alan Jones <a...@jalanjones.com> wrote: > On 12 May 2007 08:48:01 -0700, shimmyshack <matt.fa...@gmail.com> > wrote: > > > > >On May 11, 11:40 pm, Alan Jones <a...@jalanjones.com> wrote: > >> Hello everyone, any help would be greatly appreciated. > > >> What I'm trying to do may not be advisable, but here goes... > > >> I want a page named signature.php to appear conditionally as > >> an include within another include so that it will, for example, > >> appear in index.php but not in other result pages that use the > >> same top level include. > > >> The method would need to determine what page it is inside of > >> during each given instance. I guess something like... > > >> if page is index.php then include file else do nothing > > >> A 'nested conditional' seems obvious but I don't know how to > >> create an argument that checks the result page file name or > >> otherwise id's that parent page. > > >> Obviously, I'm new to PHP and my understanding of basic > >> programming is very limited. I'm also new to the group. I hope > >> to learn quickly, and I look forward to helping others in the > >> future. > > >make three php pages, > >each containing the following > >apart from the last line > ><?php > >echo '<pre>'; > >echo "me: " . __FILE__; > >echo $_SERVER["REQUEST_URI"] . "\n"; > >echo $_SERVER["SCRIPT_NAME"] . "\n"; > >echo $_SERVER["PHP_SELF"] . "\n"; > >echo $_SERVER["SCRIPT_FILENAME"] . "\n"; > >echo "\n\n"; > >include ( '2.php' ); > >?> > >inside 2.php the last line should be 3.php > >and inside 3.php theres no include > > >you will see that __FILE__ always contains the name of the script it > >is in, whether than is included in something else or not. > >whereas $_SERVER['SCRIPT_FILENAME'] doesnt change, it returns 1.php > >because that is the script your are executing, so you can use it to > >find the name of the file that includes the others. > >your basic script would be > >if( basename($_SERVER['SCRIPT_FILENAME'])=='index.php' ) > >{ > > include( 'signature.php' ); > >} > >however if you change your server setup this might not always work, as > >you said before hard coding things like this is generally not a good > >idea. Instead look at the architecture you are building and see if you > >cant handle the whole thing in one function somewhere centrally > >located say in /private/appname/functions > >this way your life is easier later on. > >hope that helps. > > Thank you very much for the help. > 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. | |||
|
| | #5 | ||
| On 12 May 2007 19:20:20 -0700, shimmyshack <matt.farey@gmail.com> wrote: >> Thank you very much for the help. >> 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... | |||
|
| | #6 | ||
| 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. > >> 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. | |||
|
| | #7 | ||
| On 13 May 2007 04:41:19 -0700, shimmyshack <matt.farey@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. >> >> 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 using http://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. over this hump, thank you. | |||
|
| | #8 | ||
| 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. > >> >> 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. > 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. | |||
|
| | #9 | ||
| On 13 May 2007 18:42:28 -0700, shimmyshack <matt.farey@gmail.com> wrote: >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. Thanks, I'll digest all that over time, as I continue learning. | |||
|
| Featured Websites | ||||
|
![]() |
| Tags: conditional, identify, nested, page, parent |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with posting using conditional if | Art | PHP | 3 | 05-20-2007 5:33 PM |
| page loading !! | Beshoo | PHP | 3 | 05-20-2007 5:33 PM |
| Xfire page | MadKad | The Games | 2 | 10-31-2006 10:07 AM |
| Featured Websites | ||||
|