I suggest you go back and read the original problem; a switch that "has about 200 lines of included files all with query string" - when filenames don't take querystrings. How's tacking on an absolute filepath going to fix that? That's what I'm asking. Until that's answered, this whole DOCUMENT_ROOT malarkey seems totally spurious.
Include with a query string
Read his message below:
He can't get his include() statements to work. This is the problem I am addressing. Thus my reference to using $_SERVER[DOCUMENT_ROOT] so that he can get a FULL ABSOLUTE PATH to his PHP files.
Understand where I am coming from?
Kerry Kobashi
Kobashi Computing
I already tried setting full urls, didnt work for some reason.
Owner, Operator, Head of IT, IT Tech, GM, Manager, and board janitor extrodinaire
nettech-computers.com
crossvilleinfo.com
Just for the record I got the include to work on the absoulte path, hardcoding the docroot, but I agree with weedpacket, we did get a little off track I dont know, I think that I am going to make a array with the var that I need to look for, the include page, and something else that i thought of earlyer but just can remember. I just hat wipeing out my 300 lines of code(only 180 or so includes, the rest is the switch code) I still am wondering why using the remote page include isnt working include("http://localhost/php/main.php?page=index") tells me that it cant find the page(I have to use localhost, my server is behind a firewall and cant dns lookup itself, but that is a whole other thread)
I dont, I have to get some sleep.
Viper:
I have used IIS 4+ on Windows Servers and can say this:
"You will run into lots of problems"
I solved ALL of my PHP problems and hassles by switching to Apache. So that is a warning!
Now regards to you getting the include file pointed at the right file, I am starting on the premise that you could NOT get the URL paths to work. Thus, I am trying to get you to use absolute file paths instead:
include("c:\InetPub\yourfilehere.php");
$_SERVER["DOCUMENT_ROOT"] returns the base root physical file path. Since this is a BUG in PHP 4.x, it DOES NOT EXIST as if you run phpInfo(), this will show this problem. There is much problems with getting the base physical root on a Windows IIS system with PHP on versions 4.x and above.
As Weedpacket mentioned, using URL's in include() is a bad idea and which I hinted at because of performance drawdowns.
There are TWO ways to set this:
a) $docRoot = "c:\InetPub" . "\yourfilename.php";
or
b) Set the doc_root in PHP.INI and THEN
$docRoot = ini_get("doc_root) . "\yourfilename.php";
Kerry Kobashi
Kobashi Computing
No we didnt get off track. Irregardless if there is 200 lines of switch code, you didn't know how to properly use the include() statement to bring in the file! ROFLMAO
His original problem stated that he couldn't get the file to be included.
Kerry Kobashi
Kobashi Computing
Just for the record I got the include to work on the absoulte path, hardcoding the docroot, but I agree with weedpacket, we did get a little off track
Here's his original message:
well I just tried it, wont work, it says that it is not found, so I tried adding a "./" to each link, no luck, then I set them to my web address "http://www.crossvilleinfo.com/php/mypage.php?info" still nothing, I have tried everything that I can think of, cant get it to include the page with a query string. What am I doing wrong???
Well, I do how to use use the include, I am just trying to keep from rewriting my giant chunck of code, Now that is nice, about the absoulte paths, but that still doesnt help my problem. I guess that I will just wipe out the file and rething my whole nav system
Your original question posed said that you tried various methods of getting the right path to a file using include(). If you knew how to use include, you wouldn't have had the problem posed in the original question!
You mentioned getting file not found errors, then you tried adding relative references, all the way to using URL paths. That discussion then headed into weedpacket bringing up fopen URL's, which we found out that this was not a good thing to do because of performance issues.
You can't run until you get the basics solved. Thus, my recommendation that you simply hard code the absolute path to the include file to get you running.
Then, somehow the topic got diverted to the switch statement problem without regards to solving the original question!!!
You should have solved the original problem FIRST, then mentioned the big switch statement... one is a problem, the other is a programming excercise.
The rest of your navigation solution is a piece of cake. Just create a associative array mapping section names to file names!
$aMap = Array(Array("About" => "About.php"),
Array("Contact" => "ContactUs.php"),
Array("Faq" => "Faq.php"),
Array("Privacy" => "Privacy.php"));
$docRoot = "C:/InetPub/"; // hard code it for now
foreach($aMap as $key => $value)
{
if ($_GET["file"] == $key)
{
// NOTICE THE USE OF $docRoot HERE TO GET THE
// ABSOLUTE PATH!
include($docRoot . $value);
break;
}
}
Good luck!
Kerry Kobashi
Kobashi Computing
All nice and funky. Now; what about those querystrings?
I've got one question to ask viper21634, first:
How do your included pages use the querystring part? Personally, I'm not familiar with querystrings that look like the ones you've got (page.php?foo), and trying it out gives me a blank $_GET[] array. I just want to know this and then I can give what I think would be a better solution than a huge switch full of repetitive code.
You're probably going to have to throw out a lot of code; but that seems inevitable anyway. Should've tried it with just one case first , eh?
Originally posted by Kerry Kobashi
His original problem stated that he couldn't get the file to be included.
Which would have been because he was trying to include a file that had a querystring attached in the manner of a URL ("Failed opening 'page.php?foo' for inclusion"). You still haven't answered how tacking on a DOCUMENT_ROOT would have fixed that. For Ralf's sake, this thread is titled "Include with a query string".
well to use the querystring i was trying to do something like this
include("page.php?index")
then on page.php
if(isset($index))
{
//code to output the page
}
now, ifeverything worked like i wnat it to, it would run through my switch till if came to the index case and include the page.php with the index defined in the query string
Right; I just needed to know that so you wouldn't have to change the included pages any.
Instead of
include('page.php?index');
, it would be
$index=true;
include('page.php');
Then, when the file page.php is included, $index would be set, the test would succeed, and the page output code would be run.
There is a risk, however, which depends on which other variables are set. Suppose you have two options in page.php - "index" and "foo". Suppose further that you were using $foo for something earlier. If you don't unset it first, then when the above file is included, the page will do its thing for both index and foo. The immediate solution would be to make sure that none of these various options are set at all before you start including pages. And the immediate solution to that would be to not use those variables in the rest of the script (this won't work if you're going through the switch more than once in a given run of the script, though).
An alternate approach would be to have a single known variable - "option", say - which could take on various values:
$option='index';
include('page.php');
and in page.php:
if($option=='index')
{
//code to output the page
}
The only drawback being that a little more thought would be needed if one wanted two variables set when page.php is included.
But anyway, the code in page.php hasn't changed. The code in the switch
has become
case 'index':
$index = true;
include('page.php');
break;
And there are something like a couple of hundred of those blocks. Heavy going, that. Something that tedious sounds like something a computer could do. And PHP can, and does. Everytime it looks up a value in an associative array it searches through array keys for one that matches - and that's exactly what you're doing here: searching through switch cases for one that matches.
Consider a separate config file instead to contain all the info. There are any number of ways of desigining a config file format, but for our purposes we might as well use 100% pure PHP; then we can just include('config.php') and PHP's own parser will do the hard work.
But what goes in the config? Well, we have a hundred or so of those case
blocks, which sounds like a job for an array.
$includes = array(
What are the elements of the array? Well, they need to store the specifics for each block: the case, the option variable, and the page name. Three items - we might as well store those in an array as well; make it an associative one so we can refer to the elements by name. We'll use the case as a key, so that we can refer to rows by case:
'index' => array('page'=>'page.php', 'option'=>'index'),
'userinfo' => array('page'=>'users.php', 'option'=>'info'),
...
And finish off the $includes array:
);
(For now, let's just test this with two pages. Once we're happy with it we can add the other 178).
Gee, we just created a database. Well, if this was likely to change with any regularity, a database would be a better choice, and it would be straightforward to convert this job to use one; but this is site navigation stuff, and that's fairly static as these things go. Let's not get too dynamic, 'cos that just slows things down.
Now that we've got our array, let's use it. First, drop the switch() statement - it's just an explicit simulation of something PHP does implicitly whenever it looks up something in an associative array. We've got an associative array now, so PHP can do all the switching we need.
Include the config array. I've suggested include('config.php'); but only in the interests of flexibility (come back to that later); it could also be inlined.
include('config.php');
Now, I've forgotten what the variable you were switching on is called. I'll just call it $switch. Replace it with whatever's correct. We want to see if there is an entry in the array corresponding to the option $switch. If there isn't we do whatever the default action is (can this happen? No harm in sticking in default behaviour with the comment CAN'T HAPPEN, I suppose...). And of course, if the $switch option is in the array, we want to include the corresponding file with the corresponding option set.
if(isset($includes[$switch]))
{ ...do the right thing.
}
else
{ // DEFAULT (Can't happen?)
}
And you can see why I used the possible values for $switch as an array key - now PHP's internals are doing all the searching and switching and all you need to do is a table lookup
Do the right thing. That would be to
$index = true;
include('page.php');
For various values of "index" and "page.php". What values? Why, those in $includes[$switch]['option'] and $includes[$switch]['page']!
${$includes[$switch]['option']} = true;
include($includes[$switch]['page']);
So all in all the switch statement in the script becomes:
include('config.php');
if(isset($includes[$switch]))
{ ${$includes[$switch]['option']}= true;
include($includes[$switch]['page']);
}
else
{ //DEFAULT CASE
}
With a config file that looks like
$includes = array(
'index' => array('page'=>'page.php', 'option'=>'index'),
'userinfo' => array('page'=>'users.php', 'option'=>'info'),
//...etc...
}
Adding a new page is a matter of a single line in the config array. No doubt there are other places in your program that use the same or related information. Additional information could be stored in the array without any hassle, and then it can be used in related situations. Careful enough planning to the exact format and contents of the array and the uses to which the information therein is put could see the whole navigation system configured from this one file; then an addition to this one array will be reflected throughout the site wherever it's appropriate.
Thanks, you have given me some good options, I think that I may either use the array method, or just break down and code it all info my dbase and yank it out when I want it. I dont know, i just know that I am tired, and about ready to sell the domain that i am working on this for :mad:
Hey look i finally made senior member, that and a quarter will get me a cup of coffie
:p :p