story97 Posted January 13, 2015 Share Posted January 13, 2015 (edited) See if I can explain this right. I am currently using static files for a very basic set of pages. Right now i have a main file that has three includes. a header include, body and footer. Due to needing to add pages, i don't want to keep adding more and more files. instead I want to use php to change one of the includes based on the url. so thissite.com/index.php?lesson1 would change this <?php include("header.php"); ?> <?php include("content-lessons.html"); ?> <?php include("footer.php"); ?> to this <?php include("header.php"); ?> <?php include("lesson1.html"); ?> <?php include("footer.php"); ?> or whichever way would be the best way to set the second include. my guess was that if the second line had something like this? if(isset($_GET['lesson1'])){ include('include/in-'.$_GET['lesson1.html'] can someone give me a starting place here? i am pretty sure i can pick ti up if someone gets me going in the right direction. thanks a ton. Edited January 13, 2015 by story97 Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/ Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 Your last example could do this for you although I would think you might use something other than 'filename.html' as the parm in the url. Maybe just a code or a word that your called script can use to lookup on a table or a static list of actual filenames and THEN execute the include. Say your current web page has some kind of action that chooses the next web page to be built and loaded. The script that it triggers does this: if (isset($_GET['file']) && $_GET['file']<>'') { $file = strtolower($_GET['file']); if (array_key_exists($file,$files_ar)) $include_name = $files_ar[$file]; else die("Invalid url value"); include('header.php'); include($include_name); include('footer.php'); exit(); } else die("Missing url value") exit(); All your pages would call this one script. Each page would be responsible for passing the next page's code/name in the url when it calls this. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502753 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) hmm Well, i tried that code and get no return. empty page not sure why. I also get no return in the console which is how i debug jquery, and see nothing in the php error logs. course, this isn't javascript so I can't really debug with firebug Edited January 13, 2015 by story97 Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502755 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 Show us your code that you developed from my little sample Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502756 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) Actually i thought taht WAS the entire code. so wrap in php and go? then the call would be site/index.php?lesson1 just so you know, i know absolutely nothing about writing php. I can recognize things, but i am not a developer. i thought you meant to construct it just like that Edited January 13, 2015 by story97 Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502757 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 No. Your question seemed knowledgeable so I made the erroneous assumption that you could follow my example. Again - show me what you ran. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502758 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) I did, i wrapped it in php and followed the url Some of the things didn't make sense to me. i thought this would just require an if statement and a _GET to see what i put after the URL, and include the corresponding file. so if the URL ended ?lessonq1 then the second include would bring in the file named lesson1, which is an html template <?php if (isset($_GET['file']) && $_GET['file']<>'') { $file = strtolower($_GET['file']); if (array_key_exists($file,$files_ar)) $include_name = $files_ar[$file]; else die("Invalid url value"); include('header.php'); include($include_name); include('footer.php'); exit(); } else die("Missing url value") exit(); ?> Edited January 13, 2015 by story97 Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502760 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 This is not what you ran. YOu have to have some webpage that triggers this script. Show me that. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502761 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 (edited) I'm not sure what you aren't undersdtanding. i put that in a blank PHP file and uploaded it the example I showed you in the first post is all the existing page is. a single file with 3 includes that bring in a header, body and footer Edited January 13, 2015 by story97 Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502762 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 I'm afraid it is you who doesn't understand. Good bye Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502763 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 yeah gee thanks, Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502764 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 maybe I'm not explaining this right. i don't know. anyone else that reads this, all i'm trying to do is set a variable so the end of the URL string defines the second include. so file.php?lesson1 would create an include for the file named lesson1. I don't know exactly what I'm not explaining right Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502765 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 You explained it perfectly. You didn't understand what I proposed to you and you don't understand how to utilize the code I gave you. I gave you something YOU could use in conjunction with your existing code but apparently you don't have any existing code. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502766 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 No, i don't. all of the code for the site is in those includes. the only thing I'm trying to accompish is to use a url string to change the name of the include but I don't know how to construct it. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502767 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 Show us your code. What don't you understand about that?????????? Jeez! Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502770 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 I've done it three times. the ENTIRE contents of the page is <?php include("header.php"); include("content-lessons.html"); include("footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502772 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 again, the only thing I can figure is I'm not explaining this right. instead of having 10 files each named differently with a different second include, i need to write a statement that will change the name of the second include based on what is on the URL since I know I can establish varibales using the URL string with ? & and = Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502773 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 You really are thick. You said "all of the code for the site is in those includes". I said show us your code. That means show us the contents of those includes. Simple, no? Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502774 Share on other sites More sharing options...
story97 Posted January 13, 2015 Author Share Posted January 13, 2015 1. it's completely irrelevant what's inside those files. it makes absolutely no difference. 2. i'm too thick, don't worry about it. i'll find the answer. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502776 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 You apparently didn't understand my original proposal for solving this problem. Perhaps a re-read would help but probably not. You don't have a good enough understanding of how web pages interact and how php can be used to act and react to events. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502777 Share on other sites More sharing options...
Barand Posted January 13, 2015 Share Posted January 13, 2015 ginerjm's code relies on your having an array of filenames ($files_ar), which he didn't show. This could be hard-coded as in my example below or you could read the list into an array from a text file. <?php $files_ar = array ( 1 => 'Lesson1.html', 'Lesson2.html', 'Lesson3.html', 'Lesson4.html', 'Lesson5.html', ); if (isset($_GET['file']) && $_GET['file']<>'') { $file = strtolower($_GET['file']); if (array_key_exists($file,$files_ar)) $include_name = $files_ar[$file]; else die("Invalid url value"); include('header.php'); include($include_name); include('footer.php'); exit(); } else die("Missing url value"); exit(); ?> You would then call the page with thissite.com/index.php?file=1 etc Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502790 Share on other sites More sharing options...
Ch0cu3r Posted January 13, 2015 Share Posted January 13, 2015 (edited) Edit: Durp Did not see Barands reply @story97 the code provided by ginerjm in post #2 will include the name of the file that is in the url query string parameter named file. However in order for the code to function correctly you first need to whitelist the files that can be included, this is prevent a user from including any file. You do this by adding them to an array called $files_ar Example $files_ar = array( 'lesson1' => 'full/path/to/lesson1.html'; 'lesson2' => 'full/path/to/lesson2.html'; ...etc ); // followed by ginerjm code The url for including lession1.html will be site.com/your_page.php?file=lession1 this will then include the file lession1.html Edited January 13, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502791 Share on other sites More sharing options...
ginerjm Posted January 13, 2015 Share Posted January 13, 2015 I think what our OP doesn't connect on is that he has to modify his existing(?) pages to pass the appropriate query string in his url to feed my code. Tried to get him to show me his code, but for some reason he couldn't. And that was that. Quote Link to comment https://forums.phpfreaks.com/topic/293882-newbie-help-including-file-based-on-url-var/#findComment-1502800 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.