Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
// indicating 'currentpage' state with PHP/CSS

Posted by tim0fee (tim0fee), 7 September 2005
Please could someone help me out here ? I am very new to PHP and need some guidance. I have been following an 'includes' tutorial here: http://www.digital-web.com/articles/easypeasy_php_2/

The downloadable sample files include a way of hi-lighting the 'currentpage' state with PHP. Here's the problem: If I move the 'include navigation.php' code to ABOVE the 'include content' bit, the indicator stops working! I suspect it is something to do with the nesting of the if/ifelse/else code - but I have tried all that I know (not much) to no avail. Can anyone help me out with an answer please?

Here's the 'includes code' that is based in index.php and calls all others - in the only order I can make the 'currentpage' PHP/CSSselector work: (If I move the line in question - the indicator stops!)

<?php
include ($_SERVER['DOCUMENT_ROOT'].'/inc/header.php');

// 1. Define an array of allowed $_GET values:
   $pass = array('intro','page2','page3','page4');

// 2. If the page is allowed, include it:
   if (in_array($_GET['id'], $pass)) {
    include ($_SERVER['DOCUMENT_ROOT'] . '/inc/' . $_GET['id'] . '.php'); //this includes the content here I think...
   }

// 3. If there is no $_GET['id'] defined, then serve the homepage:
   elseif (!isset($_GET['id'])) {
    include ($_SERVER['DOCUMENT_ROOT'] . '/inc/intro.php');
   }

// 4. If the page is not allowed, send them to an error page:
   else {
    // This sends the 404 header:
     header("HTTP/1.0 404 Not Found");
    // This includes the error page:
     include ($_SERVER['DOCUMENT_ROOT'] . '/inc/error.php');
}

// This includes the navigation bar:
include ($_SERVER['DOCUMENT_ROOT'].'/inc/navigation.php'); //this is the line in question!

// This includes the footer page:
include ($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php');

?>

And here's the 'navigation.php' code sample:

<ul>
<li><a href="index.php?id=intro"<?php if ($thisPage=="intro") echo " id=\"currentpage\""; ?>>// intro</a></li>

<li><a href="index.php?id=page1"<?php if ($thisPage=="page1") echo " id=\"currentpage\""; ?>>// one</a></li>


<li><a href="index.php?id=page2"<?php if ($thisPage=="page2") echo " id=\"currentpage\""; ?>>// two</a></li>

<li><a href="index.php?id=page3"<?php if ($thisPage=="page3") echo " id=\"currentpage\""; ?>>// three</a></li>

</ul>

And the CSS

a#currentpage {

background: #666666;
color: #cc3333;

}

And each content page has the following code at the top of each page:

<?php
     $thisPage = "page(foo)";
?>

Sorry for the long post - but I am tearing my hair out over this one. I bet the answer's simple for an expert!!

Thanks.

tim0fee


Posted by admin (Graham Ellis), 8 September 2005
on 09/07/05 at 16:02:05, tim0fee wrote:
If I move the 'include navigation.php' code to ABOVE the 'include content' bit, the indicator stops working! I suspect it is something to do with the nesting of the if/ifelse/else code - but I have tried all that I know (not much) to no avail. Can anyone help me out with an answer please?


I think the immediate answer if that the navigation.php code relies on settings made on individual pages, and as it's a program that runs from top to bottom, then the navigation code can only work when it's "below" ... in other words once the input variables it requires have been set.  

I do fear that I've given you an immediate answer to your question, but that it won't really lead you to a solution in terms of making it to what you want it to.  Looking at the article that you directed us to ... yes, I can see what they're saying and that it could work.   I'm less that keen on their structure / approach, though ...

Idea - can you take two steps back and describe in a couple of paragraphs what you're looking to achieve.   I'll put some thoughts toghether to a nice example piece of code that will do it without the buffer status trick and other "horror"s that the article uses, and I'll write a more readable example too.   Oh - I'm training tomorrow, so the example would be a weekend task.


Posted by tim0fee (tim0fee), 9 September 2005
Thanks for your reply & kind offer Graham.

It's very hard for us newbies to judge what is a code 'horror' as so many people seem to have differrent answers. But quite simply -

I would like to be able to place my 'includes code' for the various page elements anywhere & in any order to co-incide with my html/css/div flow and still have the 'currentpage' state trigger the #currentpage css indicator.

To be tied to that provided example's flow seems crazy (which originally has the navigation appear UNDER the content which is odd)!

Thanks again.

tim0fee



Posted by admin (Graham Ellis), 10 September 2005
I do understand that you'll find 10 ways described to perform a task where you had hoped for just one, and you'll find the expert areguing over which is best ... with non-experts suggesting that you do it their way "because it worked for me" which may be true enough but may lead you down a route that's not portable, not easily extensible / maintainable, or has some security implications.

OK ... I'm off my soapbox.

I think the issue you have with the original code posted is that it's generating content and style information based on that content.   Which means that code MUST be arranged - as it stands - to send out the generated content before the style.  Otherwise, it's like telling a punchline before the joke - it just can't work.

But you want style THEN content.

Mechanism to use - run the PHP code in more or less any order you like at the start of the script, but rather than printing / echoing content, save that into variable and just output at at the very end.  Demo to come in a few minutes ...

Posted by admin (Graham Ellis), 10 September 2005
OK ... have a look at http://www.wellho.net/dcms/index.php where I've put a sample set of pages.

index.php Code:
<?php

$content = <<<STUFF
This is a demonstration page that shows you how you can put
altering content into a template and use include files to
satisfy multiple URLs.<br /><br />
STUFF;

$title = "Linked page sitelet made easy";

include ("mainapp.xyz");
?>


second.php Code:
<?php

$content = <<<STUFF
The main contents of the system are on this page; this is an
appliaction that links three pages together using a single PHP
template which adapts its display to reflect the page it's on.
In this simple example, that's done just be eliminating the
link back to the page itself.<br /><br />
STUFF;

$title = "The main stuff about the application";

include ("mainapp.xyz");
?>


third.php Code:
<?php

$content = <<<STUFF
Please feel free to use this, but please be careful to check
that it's suitable for your purpose. Whilst every attempt has
been made to write  a great little script, some things have been
made a bit simple and ould do with some extra validation.<br /><br />
Learn about PHP <a href=http://www.wellho.net/course/ph.html>here!</a>
<br /><br />
STUFF;

$title = "Fine print";

include ("mainapp.xyz");
?>


and the included file mainapp.xyz Code:
<?php

# General code can be added here!
$copyyear = date("Y");

function linkto($page,$text) {
    if (! ereg("$page.php\$",$_SERVER["PHP_SELF"])) {
       print ("<a href=$page.php>$text</a><br />\n");
       }
    }

?>
<html>
<head><title><?= $title ?></title></head>
<body bgcolor="#FFFFCC">
<h1><?= $title ?></h1>
<?= $content ?>
<hr />
Navigation ...<br />
<?= linkto("index","Home Page") ?>
<?= linkto("second","Main body") ?>
<?= linkto("third","Fine print") ?>
<br />
Copyright, Graham Ellis <?= $copyyear ?><br>
This is page <a href=<?= $_SERVER[REQUEST_URI]
?>>http://www.wellho.net<?= $_SERVER[REQUEST_URI]
?></a><br>
<a href=http://www.wellho.net>Our Home Page</a>
</body>
</html>


Here's hoping I've answered the right question ... nice, easy to update and flexible little demo piece if I haven't!

Posted by tim0fee (tim0fee), 10 September 2005
Oh wow!

Thanks very much for your work & suggestions Graham. Will have a look at this closely...!

tim0fee

Posted by tim0fee (tim0fee), 12 September 2005
...erm, I'm prolly being very thick here but I can't get anything to work for me with that code. Is that the exact code you used for your demo ?

The syntax colouring in my editor (either DW or Text Wrangler) is showing mixes of clauses and  ' causing code breaks etc in two.php and three.php

I'm probably revealing my ignorance, but what is an .xyz file ? I tried renaming that to be a .php file (and updated the includes) but still a blank page on index.php

Maybe I wasn't clear enough about my aims either - What I was trying to achieve; rather than show in text the 'currentpage' (as in your live demo) - was simply to style the existing  'currentpage' nav link. ie the 'fine print' link changes background colour when that page is selected.

Sorry to come back with this list of niggles...I guess it must be me not understanding something fundamental.

tim0fee



Posted by admin (Graham Ellis), 12 September 2005
The code there is exactly as my demo piece worked ... but one of the issues with PHP is that it's highly configurable and in a couple of instances the configuation can effect the code.   In particular:
1) I have short tags enabled to support the <?= ....  ?> syntax
2) Apache is set up to parse .php files as the PHP extension.

The ".xyz" extension shouldn't matter ... it's just something I use from time to time to indicate a file that's never going to map to a "mime type".   In other words something that you'll always be offered "save to disc" for if you call it in a browser - that's all.

I'm not surprised about your syntax highlighter. The <<< notation is a great way of inserting a whole block of text, but it can confuse.   It was the author of Perl, Larry Wall, who famously said "the only program that can interpret Perl IS Perl" ... and the same rather applies to PHP too!

I understand your comment about just looking to work with a page with a changing link.   Understood.  But you'll keep running into sequencing options with that route, as you saw.  You'll also find yourself repeating a lot of code and work.

Why didn't it work for you?  I don't know.  If it's not one of the obvious things I suggested at the top, please post a follow up telling me how it fails and I may be able to offer some tuned suggestions.  "Syntax error at line 15" or "It offers links but they're all broken" will give me a bit more to go on than "can't get anything to work for me"  

Posted by curtis (curtis), 8 October 2005
Lately, I've been working on a template class, which has already neared 500 lines, and I've only got the basics! It allows you to create pages that look like this:

Code:
<html>
<head><title>{TITLE}</title></head>
<body>
{CONTENT}

<!--* IF template_var == 'some value' *-->
Outputs only if condition is satisfied
<!--* ENDIF *-->

<!--* INCLUDE 'path/to/file.html' *-->

{FOOTER}

I plan to have a lot of aspects that will make a pretty nice PHP API-like interface for creating dynamic content. I worked out IF statements and in-template includes. If you're wondering, I did NOT use eval() for anything and don't intend to either.

Anyway, hopefully it'll be useful and portable enough for me down the line

Posted by admin (Graham Ellis), 8 October 2005
PHP is - err - in itself a templating system.  By which I mean you create a page template (HTML) and into it you insert bits of PHP.  I'm a little nervous of writing a complete templating system within what's already a templating system.

Have you ever listed to Rasmus Lerdorf (originator of PHP and still VERY much "with" the language) talk?  I had the pleasure last year; it was fascinating to listen to the philosopy of the system as well as hearing about nuts and bolts and his challenge "Why write a templating system within a templating system" is a good one; better to extend the existing system and make best effective use of it.

The four layer model, where the top level (which is the page actually held atthe called URL) brings in the HTML template itself, web helper code, and business logic is very much the approach that Rasmus was advocating, and I have to say I'm in agreement with him on this;  have a look at the sc5 and sc6 demo on our "best practise" page for an examples, and at our training notes for the full text describing them.

Posted by curtis (curtis), 8 October 2005
Hey Graham, thanks for that info!

Now that you mention it, it does seem redundant to have a special template syntax, when PHP is more than capable. I may just aim my template class to provide commonly used markup, like I made a drop down box function a while ago, so I may include that and some other stuff.

I guess you really need to think about how you want your site to look and about your various needs.

Thanks again Graham,
Curtis

Posted by admin (Graham Ellis), 8 October 2005
on 10/08/05 at 19:25:29, curtis wrote:
I may just aim my template class to provide commonly used markup, like I made a drop down box function a while ago .....


Indeed - and that's so much the "web helpers" philosophy.  I've found it works well for me - a solid grounding of common routines right across the web site.

Posted by curtis (curtis), 11 October 2005
Thanks again Graham, it took me a while, but when you look at:

Code:
<!-- INCLUDE 'file' -->
Compared to
<?php include_once('file'); ?>


It doesn't really make sense to waste time on stuff like that . Well, when I get some content going for my site, and nice interactive stuff going, I'll be sure to check back and share anything that might be of interest.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

3 unpublished comments pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho