Smith Posted March 27, 2007 Share Posted March 27, 2007 First of all I must apologise for the pathetic simplicity of my problem, I am not accustomed to using php or html other than for very basic purposes. I wish to use the following in a simple web page: <php include( $page ".txt") > I have tried different minor variations of it but unfortunately I continue to recieve the following message: "Notice: Undefined variable: page in /home/fhlinux172/f/fmpocketguide.co.uk/user/htdocs/index.php on line 97 Warning: include(.txt) [function.include]: failed to open stream: No such file or directory in /home/fhlinux172/f/fmpocketguide.co.uk/user/htdocs/index.php on line 97 Warning: include() [function.include]: Failed opening '.txt' for inclusion (include_path='.:/usr/share/pear-php5') in /home/fhlinux172/f/fmpocketguide.co.uk/user/htdocs/index.php on line 97" I'm sure there will be a glaring error with the little bit of code or some other simple explanation, sadly I just don't know Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/ Share on other sites More sharing options...
ted_chou12 Posted March 27, 2007 Share Posted March 27, 2007 put your txt file and this php file in the same directory; code: <?php include("$page.txt"); ?> Ted Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216008 Share on other sites More sharing options...
mjlogan Posted March 27, 2007 Share Posted March 27, 2007 <?php include( $page.".txt"); ?> and the error says that $page isnt defined anywhere, so it wont know what page to load and will fail. Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216009 Share on other sites More sharing options...
Smith Posted March 27, 2007 Author Share Posted March 27, 2007 Yep, unfortunately still no luck. As you may have guessed, I have no idea how to define $page in order for this to work properly. Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216011 Share on other sites More sharing options...
ted_chou12 Posted March 27, 2007 Share Posted March 27, 2007 what is the name of your text file? Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216013 Share on other sites More sharing options...
Smith Posted March 27, 2007 Author Share Posted March 27, 2007 home.txt Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216014 Share on other sites More sharing options...
mjlogan Posted March 27, 2007 Share Posted March 27, 2007 <?php $page = "home"; include( $page.".txt"); ?> Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216020 Share on other sites More sharing options...
ted_chou12 Posted March 27, 2007 Share Posted March 27, 2007 if that is so: <?php include("home.txt"); ?> isnt this much simpler? Ted Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216025 Share on other sites More sharing options...
mjlogan Posted March 27, 2007 Share Posted March 27, 2007 it is but i was demonstrating assigning variables. Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216035 Share on other sites More sharing options...
Smith Posted March 27, 2007 Author Share Posted March 27, 2007 Thank you for your help. I was hoping however to be able to point to various files, not just home.txt e.g. news.txt, about.txt etc. Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216039 Share on other sites More sharing options...
ted_chou12 Posted March 27, 2007 Share Posted March 27, 2007 yeah, in that case, follow mjlogan's previous post. Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216040 Share on other sites More sharing options...
Smith Posted March 27, 2007 Author Share Posted March 27, 2007 I did use mjlogan's code, but upon attempting to view /index.php?page=test I still only see that home.txt file. I appreciate that this is because $page has now been defined as "home", I just don't know how to alter the code to make it do what I want. I do apologise for my complete lack of understanding here Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216042 Share on other sites More sharing options...
ted_chou12 Posted March 27, 2007 Share Posted March 27, 2007 <?php $page = $_GET['page']; include("$page.txt"); ?> Ted Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216046 Share on other sites More sharing options...
Smith Posted March 27, 2007 Author Share Posted March 27, 2007 That's perfect. Thank you all very much for your help. Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216047 Share on other sites More sharing options...
Smith Posted March 27, 2007 Author Share Posted March 27, 2007 Sorry for the double post, but how would I assign a default page into this code so that when viewing index.php, a specific .txt file is displayed? Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216051 Share on other sites More sharing options...
mjlogan Posted March 27, 2007 Share Posted March 27, 2007 <?php if (isset($_GET['page'])){ $page = $_GET['page']; } else { $page = "index"; } include("$page.txt"); ?> or slightly more advanced. if (!isset($_GET['page'])){ $page = "index"; }elseif (preg_match ("/[^A-z]/", $_GET['page']) || !file_exists("" .$_GET['page']. ".txt")){ $page = "error"; } else { $page = $_GET['page']; } Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216053 Share on other sites More sharing options...
Smith Posted March 27, 2007 Author Share Posted March 27, 2007 Thanks again Link to comment https://forums.phpfreaks.com/topic/44474-basic-include-problem/#findComment-216054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.