renfley Posted February 25, 2010 Share Posted February 25, 2010 K hey guys hoping one of you can help me out lol, Problem= When someone clicks a link it directs them to index.php?page=home <ul> <li><a href="index.php?page=home">Home</a></li> <li><a href="index.php?page=about">About us</a></li> <li><a href="index.php?page=contact">Contact us</a></li> </ul> What im trying to achieve is since it the same page i wanna display different content based on the page= ? i was wondering if someone could write something quick basicly <?php (verify to see if there is something after index.php ex: ?page=home) if there isnt echo 'welcome to my site'; and if there is somethign there echo 'include/page/contact.php'; ?> Srry if this sound messed up lol Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/ Share on other sites More sharing options...
Deoctor Posted February 25, 2010 Share Posted February 25, 2010 u can actually do one thing change that to index_new.php and then in the index_new.php u can write something like this <?php include("index.php"); ?> so what it will do is that when ever some one cliks on the link it will send the request to index_new.php file and that in turn pass the values back to the index.php so in the top of index page u can write something like this $Page=$_GET['page']; Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017857 Share on other sites More sharing options...
nilansanjaya Posted February 25, 2010 Share Posted February 25, 2010 use something like this "varname" is the part u have after "index.php?" if(isset($_GET['var_name'])){ //do what you want if there's something after "?" }else{ //if not...then here... } Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017858 Share on other sites More sharing options...
Tazerenix Posted February 25, 2010 Share Posted February 25, 2010 actually you shouldnt really do that because then the user could set the url as http://www.example.com/index.php?page=phpfreaksisawesome and it wouldnt load the index even tho thats not a real page Try: <?php $page = $_REQUEST['page']; if ($page == "contact") { require('contact page'); } else if ($page == "about") { require('about page'); } else if (($page == "home") || (!$page)) { // your code for the home page } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017869 Share on other sites More sharing options...
nilansanjaya Posted February 25, 2010 Share Posted February 25, 2010 actually you shouldnt really do that because then the user could set the url as http://www.example.com/index.php?page=phpfreaksisawesome and it wouldnt load the index even tho thats not a real page Try: <?php $page = $_REQUEST['page']; if ($page == "contact") { require('contact page'); } else if ($page == "about") { require('about page'); } else if (($page == "home") || (!$page)) { // your code for the home page } ?> yeah...good point ! Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017877 Share on other sites More sharing options...
alpine Posted February 25, 2010 Share Posted February 25, 2010 Its better to use a switch statement tho <?php switch($_GET['page']){ case 'home': // include or whatever on home break; case 'news': // include or whatever on news break; default: // include or whatever when $_GET['page'] doesnt match other case, or dont exist } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017881 Share on other sites More sharing options...
renfley Posted February 25, 2010 Author Share Posted February 25, 2010 ALPINE You got the ball lol so ive changed this <?php $page = $_REQUEST['page']; if ($page == "home") { include ('pages/home.php'); } else if ($page == "products") { include ('pages/products.php'); } else if ($page == "about") { include ('pages/about.php'); } else if ($page == "order") { include ('pages/order.php'); } else if ($page == "contact") { include ('pages/contact.php'); } else if (($page == "home") || (!$page)) { // your code for the home page echo 'welcome home'; } else { echo 'hello'; } ?> The next issue is that because when the page loads its only index.php and not (index.php?page=home) It give me Notice: Undefined index: page in C:\wamp\www\Store Front\index.php on line 66 welcome home Any suggestions Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017908 Share on other sites More sharing options...
Deoctor Posted February 25, 2010 Share Posted February 25, 2010 u can try my suggestion.. Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017911 Share on other sites More sharing options...
renfley Posted February 25, 2010 Author Share Posted February 25, 2010 i think it would work if i could <?php include ('index.php?page=home'); ?> But i cant Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017914 Share on other sites More sharing options...
renfley Posted February 25, 2010 Author Share Posted February 25, 2010 If you are wondering found a quick solution that will save on code space lol <?php $page = $_GET['page']; if ($page) { include ("include/".$page.".php"); } else { echo 'Welcome to home page'; } ?> Only problem is the original Notice: Undefined index: page in C:\wamp\www\Store Front\index.php on line 66 Welcome to home page Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017916 Share on other sites More sharing options...
nilansanjaya Posted February 25, 2010 Share Posted February 25, 2010 use isset() to check weather that variable is set or not Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017918 Share on other sites More sharing options...
renfley Posted February 25, 2010 Author Share Posted February 25, 2010 You mean : <?php $page = (isset($page)) if ($page) { include ("include/".$page.".php"); } ???? Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017920 Share on other sites More sharing options...
nilansanjaya Posted February 25, 2010 Share Posted February 25, 2010 easy to do like this <?php if (isset($_GET['page'])) { include ("include/".$_GET['page'].".php"); } else { echo 'Welcome to home page'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017930 Share on other sites More sharing options...
Wolphie Posted February 25, 2010 Share Posted February 25, 2010 Alpines solution is currently the most widely-used and elegant way of doing what you're trying to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1017931 Share on other sites More sharing options...
Tazerenix Posted February 25, 2010 Share Posted February 25, 2010 yea, there is just something about switches i dislike for some reason lol. And in your code set page to if (isset($_GET['page']) $page = $_GET['page']; Quote Link to comment https://forums.phpfreaks.com/topic/193314-url-_get/#findComment-1018234 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.