ajl80
ajl80

Reputation: 43

Dynamic $_GET[$value] - any ideas?

has anyone got a work around for this?

I have got a htaccess rule targeting L0, L1, L2, and so on... as page id's (index.php?L0=example&L1=page1 and renames to example/page1.html) I want to add additional get vars example/page1.html?filter=event

I thought I could do this

$i=0;
foreach ($_GET['L'.$i] as $key => $value) {
$pages[] = $value;  
$i++;  
}
$page_id = end($pages);

this doesn't work.

this would put the pages into its own array, I could then target the additional vars separately.

at present I get the page I want using 'end($_GET)' but it conflicts if I add additional get vars.

Upvotes: 0

Views: 118

Answers (4)

ajl80
ajl80

Reputation: 43

Thanks guys, I actually figured it out whilst waiting for a response, whether its the right way. but it works.

$i=0;
foreach ($_GET as $key => $value) {
   if($key=='L'.$i){
     $pages[] = $value;
   }  
$i++;  
}
$page_id = end($pages);

Upvotes: -1

Iván Pérez
Iván Pérez

Reputation: 2539

What about this?

$i = 0;
while (isset($_GET['L' . $i])) {
    $key = 'L' . $i;
    $value = $_GET[$key];
    $pages[] = $value;
    $i++;
}

Your code only iterates through $_GET['L0'], and will fail if that is not array. So you can iterate over $_GET and use only those keys which start with L, or get those values the way I suggest.

Upvotes: 0

Martin
Martin

Reputation: 22760

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.

Upvotes: 1

iosifv
iosifv

Reputation: 1219

You need to get the entire GET data into an array and then you can process it easily. This will give you a better understanding of the issue and you will probably have to change your strategy of getting L0, L1 as they are not GET data.

Upvotes: 1

Related Questions