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.