Stefan83 Posted November 8, 2013 Share Posted November 8, 2013 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'; } Quote Link to comment Share on other sites More sharing options...
BrodaNoel Posted November 8, 2013 Share Posted November 8, 2013 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. Quote Link to comment Share on other sites More sharing options...
Stefan83 Posted November 8, 2013 Author Share Posted November 8, 2013 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']]; Quote Link to comment Share on other sites More sharing options...
BrodaNoel Posted November 18, 2013 Share Posted November 18, 2013 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'; Quote Link to comment Share on other sites More sharing options...
Stefan83 Posted November 28, 2013 Author Share Posted November 28, 2013 (edited) 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 November 28, 2013 by Stefan83 Quote Link to comment Share on other sites More sharing options...
BrodaNoel Posted November 28, 2013 Share Posted November 28, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.