|
| |
|
|
womble
Posts: 5702 Joined: 3/14/2005 From: Living on the edge Status: offline
|
Dynamic php includes script - 2/3/2006 6:34:21
A nice little script for switching php includes that you could use for site navigation, glossaries, FAQs etc. Because it's server-side php there's no worries about javascipt being switched off or anything, accessible because you're just using normal hyperlinks to switch between includes (I think the dynamic bit would probably be okay with screenreaders but as Fangs for FF isn't yet updated for FF1.5 I haven't been able to check that out), and valid (as long as the code in your include's valid ;)), just a few lines of php, so you can use it with any flavour of html/xhtml. http://www.jh-designs.com/?id=tutorials&page=comment&oid=34
_____________________________
~~ "A cruel god ain't no god at all" ~~ ~~ Erase hate. Practice love. ~~
|
|
|
|
womble
Posts: 5702 Joined: 3/14/2005 From: Living on the edge Status: offline
|
RE: Dynamic php includes script - 10/13/2006 19:03:13
UPDATE: I recently had a PM from Kitka asking if I had the code for this type of PHP include, as it appears that the original link I posted is no longer there. I've since found partically the same tutorial here, and reminded of just how easy it is to use, working on a site at the moment and I decided to use the same method on a page. <grin> No problems, done it once, piece of cake - no problems! Easy, yes? Erm, no.... It took me two hours to figure out why exactly the same syntax wasn't working! Two hours of changing single quotes to double quotes, moving semi-colons, then moving them back because though my knowledge of PHP isn't great, I *knew* they were in the right place. The syntax was identical - I even copied the original page from the previous site I'd used it on, onto the server where the site currently under construction resides. Works on the original site...not the new one. Two hours I've been messing around with it! Then I dimly recalled an email from the hosts of the currently under construction site, something about "register_globals" I seemed to recall - so I dug it out and had a look, and off a-googling again I went, and eventually after much muttering obcenities at the screen, found this thread at CSS Creator. The syntax of creating a dynamic PHP is easy - you need links to be able to click to change the content, and case/breaks to change the content when the link is clicked...: <p class="glosslink"><a href="glossary.php?id=glossary_a">A</a> ◊ <a href="glossary.php?id=glossary_b">B</a> ◊ <a href="glossary.php?id=glossary_c">C</a> ◊
<a href="glossary.php?id=glossary_d">D</a> ◊ <a href="glossary.php?id=glossary_e">E</a></p>
<?php switch($id) { default: include('http://www.mydomain.com/includes/gloss_default.php');
break; case "glossary_a": include('http://www.mydomain.com/includes/glossary_a.php');
break; case "glossary_b": include('http://www.mydomain.com/includes/glossary_b.php');
break; case "glossary_c": include('http://www.mydomain.com/includes/glossary_c.php');
break; case "glossary_d": include('http://www.mydomain.com/includes/glossary_d.php');
break; case "glossary_e": include('http://www.mydomain.com/includes/glossary_e.php');
Two points for anyone that wants to try this and is running on a server with register_globals off, that these articles don't tell you, is that if register_globals are switched off on your domain, - you need to add this after the PHP tag but before the switch statement $id = $_GET["id"]; - you need to use double quotes throughout, rather than single quotes, so: <?php $id = $_GET["id"];
switch($id) { default: include("http://www.mydomain.com/includes/gloss_default.php");
break; case "glossary_a": include("http://www.mydomain.com/includes/glossary_a.php");
break; case "glossary_b": include("http://www.mydomain.com/includes/glossary_b.php");
break; case "glossary_c": include("http://www.mydomain.com/includes/glossary_c.php");
break; case "glossary_d": include("http://www.mydomain.com/includes/glossary_d.php");
break; case "glossary_e": include("http://www.mydomain.com/includes/glossary_e.php"); Hopefully that might be of some use to anyone who decides to try dynamic includes but can't get them to work, or has includes that have suddenly stopped functioning. I know all this global_registers malarky is all in the name of security - but just grrrr!!!!!
_____________________________
~~ "A cruel god ain't no god at all" ~~ ~~ Erase hate. Practice love. ~~
|
|
|
|
Kitka
Posts: 2520 Joined: 1/31/2002 From: Australia Status: offline
|
RE: Dynamic php includes script - 10/13/2006 19:31:11
A couple of technical details: quote:
I had to use absolute urls I have it functioning nicely with relative URLs, so test to see which works best for you/your server. quote:
- you need to use double quotes throughout, rather than single quotes, so: I have used double quotes everywhere except in the following part of the code, and it works well. case "file": include('incl/file.php');
_____________________________
Kitka **It is impossible to make anything foolproof because fools are so ingenious.**
|
|
|
|
BobbyDouglas
Posts: 5470 Joined: 5/15/2003 From: Arizona Status: offline
|
RE: Dynamic php includes script - 10/13/2006 20:14:50
quote:
Only problem I had with it was that I had to use absolute urls - it didn't like the relative ones - Are you sure the relative path was correct? I don't understand why PHP wouldn't be able to include something using a relative URL? Lots of php scripts use relative URLs for includes. If you don't specify the full location to the php file, then every script you use will be using a relative link for the include. Here's how I use includes and such for navigation type pages: For the Service page: <?php $page="Service"; include("main-navigation.php"); ?> and then in my main-navigation.php page I have: <li><a href="/Service-Department/">Service Department</a><?php if ( $page=="Service" ) {echo '
<ul id="subnavlist">
<li class="both"><a href="/Service-Department/Submit-Service-Request/">Submit Service Request</a></li>
</ul>';} ?>
</li> So if the page you are on is part of the service dept., then it will show the submenu of Service Department on the navigation area. I also use this type of include for the title/head sections of pages: So for index.php > <?php $page_title="Website Name - Home"; include("header.php"); ?> and then header.php > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" >
<head>
<title><?php echo $page_title; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/scripts/address.js"></script>
<?php if($page_title=="This script should only load for a specific page") { echo
'<script type="text/javascript" src="/scripts/lightbox.js"></script>
<link href="/lightbox.css" rel="stylesheet" type="text/css" />';} ?>
</head> Notice that not every page loads the lightbox script. quote:
you need to use double quotes throughout, rather than single quotes, so: - You can use double or single quotes. I tend to use single quotes when working with HTML, reason being is that if you use a double quote, you will have to place a \ behind every double quote that appears in your HTML code. <?php if($some_var) { echo "<h1 class=\"h1_norm\">";} ?> Echos <h1 class="h1_norm"> <?php if($some_var) { echo "<h1 class='h1_norm'>";} ?> Echos <h1 class='h1_norm'> <?php if($some_var) { echo '<h1 class="h1_norm">';} ?> Echos <h1 class="h1_norm"> <?php if($some_var) { echo '<h1 class=\"h1_norm\">';} ?> Echos <h1 class="h1_norm"> <?php if($some_var) { echo "<h1 class="h1_norm">";} ?> Gives error
< Message edited by BobbyDouglas -- 10/13/2006 20:20:11 >
_____________________________
Arizona Web Design - Mr Bobs Web Design in Arizona The Arizona Web Hosting Challenge
|
|
|
|
treetopsranch
Posts: 1141 From: Cottage Grove, OR, USA Status: offline
|
RE: Dynamic php includes script - 10/13/2006 22:52:30
wombie, that link doesn't seem to be working now.
_____________________________
Don from TreeTops Ranch, Oregon "I've got a taste for quality and luxury"
|
|
|
|
Kitka
Posts: 2520 Joined: 1/31/2002 From: Australia Status: offline
|
RE: Dynamic php includes script - 10/13/2006 23:01:37
Hi Don, Do you mean the link in Womble's first post, or the one in post #4? We already know that #1 isn't working but this link in post #4 works for me right now: http://www.digital-web.com/articles/easypeasy_php
_____________________________
Kitka **It is impossible to make anything foolproof because fools are so ingenious.**
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|