684425 Posted December 15, 2014 Share Posted December 15, 2014 I have the following three links in same page. http://localhost/folder/file.php?x=link1 http://localhost/folder/file.php?x=link2 http://localhost/folder/file.php?x=link3 i want to show URLs as they are... link1 is to include a file named "x.php" link2 is to include a file named "y.php" link3 is to include a file named "z.php" Can it be done in php only? PS: its not about creating hash ids. and i want to do it without any sql db connection. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 15, 2014 Share Posted December 15, 2014 You mention 3 links. Then you mention 3 URLs. What is the connection or isn't there one? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted December 15, 2014 Share Posted December 15, 2014 I did this to be a little safer using parameter values in an include <?php //creates the links array $links = array( "link1", "link2", "link3" ); //loop as href links foreach ($links as $link) { echo "<a href='?x=$link'>$link</a> "; } //GET from address bar //only allow one that's in links array if (isset($_GET['x']) && in_array(trim($_GET['x']), $pages)) { $location = trim($_GET['x']); } else { $location = 'link1';//default location } //assign the files name to link switch ($location) { case "link1": $filename = "x"; break; case "link2": $filename = "y"; break; case "link3": $filename = "z"; break; } //so can see it work echo "<h2>" . $location . "</h2>"; //include a page $script = dirname(__FILE__) . DIRECTORY_SEPARATOR . $filename . ".php"; if (file_exists($script)) { include($script); } else { echo "$script does not exist."; } ?> Quote Link to comment Share on other sites More sharing options...
684425 Posted December 15, 2014 Author Share Posted December 15, 2014 You mention 3 links. Then you mention 3 URLs. What is the connection or isn't there one? My mistake. links = hyperlinks = URLs Quote Link to comment Share on other sites More sharing options...
684425 Posted December 16, 2014 Author Share Posted December 16, 2014 I did this to be a little safer using parameter values in an include Thanks dear for your sharing. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 16, 2014 Share Posted December 16, 2014 So - perhaps you can re-write your question using the same terms throughout to make it clearer what you want to do Quote Link to comment Share on other sites More sharing options...
684425 Posted December 19, 2014 Author Share Posted December 19, 2014 So - perhaps you can re-write your question using the same terms throughout to make it clearer what you want to do I did not know there is a time limit for editing a post. (misunderstanding sometimes because i am weak in english) The question was, i want to make URLs like; http://domain.com/x=home http://domain.com/x=account&r=profile http://domain.com/x=account&r=news&id=345 http://domain.com/x=account&r=updates&id=567 x is used for navbar and r is used for any content on account page. I asked that can it be done using php only.? without any database connection. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted December 19, 2014 Share Posted December 19, 2014 The answer is yes, but you need to associate whatever GET requests you want to the files you want to include in addition to other files or data (content in an array?) The example given is similar, you could expand onto it in the same way. Is there a specific reason why you can't use a database? It's the best way to save and display data. Quote Link to comment Share on other sites More sharing options...
684425 Posted December 21, 2014 Author Share Posted December 21, 2014 The answer is yes, but you need to associate whatever GET requests you want to the files you want to include in addition to other files or data (content in an array?) because i am poor in english, maybe i didn't get what you exactly asked. but i will try to explain what i am trying to do. URL from my last reply http://domain.com/x=account&r=news&id=345 so, x=account (include account.php) &r=news(include news.php within account.php) &id=345 (get content from database and display) The example given is similar, you could expand onto it in the same way. i did copy that example and tried to modify it but failed. because i am new to php. also i found a (mistake?) in your example. //variable $pages not defined. //i changed $pages to $links and it worked fine. if (isset($_GET['x']) && in_array(trim($_GET['x']), $pages)) { What i wanted is ... <a href="get.php?x=news">get1</a> <a href="get.php?x=updates">get2</a> <?php $title = "Home | page"; if (isset($_GET['x'])) { if ($_GET['x'] == "news"){ $title = "News | Page"; include "news.php"; } elseif ($_GET['x'] == "updates"){ $title = "Updates | Page"; include "updates.php"; } } echo "<title>$title</title>"; ?> but this is for GET x only. how can i add GET r ?? Is there a specific reason why you can't use a database? It's the best way to save and display data. I am using a database dear. I have explained above. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 21, 2014 Share Posted December 21, 2014 how can i add GET r ?? By using $_GET['r] You will do another if/else statement to include the appropriate file. You maybe better of creating a function so you pass $_GET['x'] and $_GET['r'] to it and it returns the path to the file(s) to be included. Quote Link to comment Share on other sites More sharing options...
684425 Posted December 22, 2014 Author Share Posted December 22, 2014 Someone suggested to use switch case but i don't know how to do it. any help? Quote Link to comment Share on other sites More sharing options...
684425 Posted December 25, 2014 Author Share Posted December 25, 2014 By using $_GET['r] You will do another if/else statement to include the appropriate file. You maybe better of creating a function so you pass $_GET['x'] and $_GET['r'] to it and it returns the path to the file(s) to be included. Please guide me how to do this? If you can share an example? Quote Link to comment Share on other sites More sharing options...
684425 Posted December 28, 2014 Author Share Posted December 28, 2014 <a href="get.php?x=news">get1variable</a> <a href="get.php?x=news&r=links">get2variables</a> <?php $title = "Home | page"; if (isset($_GET['x'])) { if ($_GET['x'] == "news"){ $title = "News | Page"; include "news.php"; } } if (isset($_GET['x']) && isset($_GET['r'])) { //not working if ($_GET['x'] == "news" && $_GET['r'] == "links"){ //not working $title = "Links | Page"; include "files/links.php"; //confused here, because this i think is to include links.php directly, } //but i want links.php to be included in news.php } echo "<title>$title</title>"; ?> i also tried... if (isset($_GET['x'])) { if ($_GET['x'] == "news"){ if(isset($_GET['r'])) { if ($_GET['r'] == "links"){ //this also not working $title = "Links | Page"; include "files/links.php"; //confused here... } } } } Aalso for creating a function i tried to learn how to create functions. but after learning i failed to create a function for this. if someone please guide me in detail for this? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted December 29, 2014 Share Posted December 29, 2014 If you want to include other files in certain scripts should add elseif or a switch in just those included scripts. (makes it easier to do as well) Do the check for the url parameter as shown in the script below....but only in pages you need it. Made this anyway for you to try and look at. This includes the links pages only if news is set Is more complicated because I added the switches so can rename url parameters or the included file names (I kept them named the same in example) <?php $x_pages = array( "home", "about", "news" ); $r_pages = array( "link1", "link2", "link3" ); //loop x as href links foreach ($x_pages as $x) { echo "<a href='?x=$x'>$x</a> "; } //GET x from address bar //only allow one that's in x_pages array if (isset($_GET['x']) && in_array(trim($_GET['x']), $x_pages)) { $x_location = trim($_GET['x']); } else { $x_location = 'home'; //default location } //assign the files name to parameter value switch ($x_location) { case "home": $x_filename = "home"; break; case "about": $x_filename = "about"; break; case "news": $x_filename = "news"; break; }//end x_location switch echo "<h2>" . ucfirst($x_location) . "</h2>"; //include a page $x_script = dirname(__FILE__) . DIRECTORY_SEPARATOR . $x_filename . ".php"; if (file_exists($x_script)) { include($x_script); //link pages only included in news if ($x_location == "news") { if (isset($_GET['r']) && in_array(trim($_GET['r']), $r_pages)) { $r_location = trim($_GET['r']); } else { $r_location = "link1";//default link page } echo "<div>"; foreach ($r_pages as $r) { echo "<a href='?x=news&r=$r'>$r</a> "; } echo "</div>"; echo "<h2>" . ucfirst($r_location) . "</h2>"; switch ($r_location) { case "link1": $r_filename = "link1"; break; case "link2": $r_filename = "link2"; break; case "link3": $r_filename = "link3"; break; } //end r_location switch //include link pages $r_script = dirname(__FILE__) . DIRECTORY_SEPARATOR . $r_filename . ".php"; if (file_exists($r_script)) { include($r_script); } else { echo "$r_script does not exist."; } } //end if get news } else { echo "$x_script does not exist."; } ?> To learn about functions 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.