Jump to content

Show different content based on query string


Stefan83

Recommended Posts

Hi - I'm trying to display different content based on the query string variable.

 

for example if URL contains domain.com/?from=/page1 show content1 else if URL contains domain.com/?from=/page2 show content2. This is what I have so far but I'm not sure what's wrong. Any ideas?

if ( (isset($_GET['from']) && $_GET['from'] == '/page1'))
{
 echo 'content1';
}

elseif( (isset($_GET['from']) && $_GET['from'] == '/page2'))
{
 echo 'content2';
}

else {
echo 'default';
}

You can define an array with the "map" of key-content.

 

For example:

$mapping = array('/page1' => 'content1',
                 '/page2' => 'content2',
                 '' => 'default');

echo $mapping[$_GET['from']];

So, you only do add another element in the array.

Hi BRodaNoel

 

Thanks for your reply! That's great. How do I replace the echo with include? I can't get this to work:

$mapping = array('/page1' => 'inc/mobile/content1.php',
                 '/page2' => 'inc/mobile/content2.php',
                 '' => 'inc/mobile/default.php');

include $mapping[$_GET['from']];
  • 2 weeks later...

Exactly... And with that, you solve the problem.

 

But, you need think what gonna happen when $_GET['form'] will take a value that is not in the array... The "include" sentence will include this file: "" (empty string)...

 

You should do any like that:

if($mapping[$_GET['from']] != '')
    include $mapping[$_GET['from']];
else
    include 'default_file.php';
  • 2 weeks later...

Thanks, BrodaNoel

 

Unfortunately this only displays the default.php content.

 

So to confirm. If the URL is www.domain.com/mobile/?from=/booknow, the booknow.php file should be displayed, else default.php

$mapping = array(
		'/booknow' => 'inc/mobile/booknow.php',
                 );

if($mapping[$_GET['from']] != '')
    include $mapping[$_GET['from']];
else
    include 'inc/mobile/default.php';

Where am I going wrong?

 

Thanks

Try with that:

$mapping = array(
    '/booknow' => 'inc/mobile/booknow.php', // Warning here... Remove this ","...
);

if(array_key_exists($_GET['from'], $mapping))
    include $mapping[$_GET['from']];
else
    include 'inc/mobile/default.php';

That will work fine.

 

If you still having problems, do that:

$mapping = array(
    '/booknow' => 'inc/mobile/booknow.php'
);

var_dump($mapping);
var_dump($_GET['from']);
var_dump($_GET);
var_dump(array_key_exists($_GET['from'], $mapping));
var_dump($mapping[$_GET['from']]);

if(array_key_exists($_GET['from'], $mapping))
    include $mapping[$_GET['from']];
else
    include 'inc/mobile/default.php';

And paste here all this results, so, we can see what is the problem.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.