PART 1 -- Incorrect Format to start with.
If you want to use the foreach structure, you need to ensure that the values passed to it are an array. Currently you are passing a non-array value to the foreach
so it is never firing.
You should restructure your $_GET
values so that instead of using a (manual) numeric indicator that you use an array indicator --
$_GET['L0'],
$_GET['L1']
Becomes
$_GET['L'][...]
So your URL would be file.php?L[]=value1&L[]=value2
etc. Then PHP can process these very tidily as a foreach loop.
See How to get PHP $_GET array?
Part 2 - Getting the value you want
This above value would/could then be processed as:
foreach ($_GET['L'] as $value) {
$pages[] = $value;
}
$page_id = end($pages);
This means there's no need to worry about "counting" as it's all automatic and self contained. It also will (probably) make the .htaccess value assigning somewhat easier as the $_GET array name will always be the same (in this case, L[]=
).
But in order to get the correct value you need to assign it an identifier - or a Key, say you're looking for a filter
value - What you do is manually check the "key" is the one you're looking for, so you do not define the GET array with all the same L<number>
but for example using your foreach loop again, this time defining what the key is:
So given the URL query:
page.php?L[]=var1&L[filter]=var2&L[]=var3
The foreach
would then be rewritten as:
foreach ($_GET['L'] as $key => $value) {
if ($key == "filter"){
$pages[$key] = $value;
}
else {
$pages[] = $value;
}
}
$page_id = $pages['filter'];
So then the filter
value is defined if it is present. this means that after this process the value of the filter in PHP is $pages['filter']
- clearly defined if present.