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';
}
Link to comment
Share on other sites

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']];
Link to comment
Share on other sites

  • 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';
Link to comment
Share on other sites

  • 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

Edited by Stefan83
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.