scottieclark Posted June 7, 2010 Share Posted June 7, 2010 Hi... Firstly, I have to declare...I have no idea what I'm doing! The goal: --------------------------------------------------------------------------------- To load text in html div tags dynamically, loaded from a .txt file. --------------------------------------------------------------------------------- So far... I've spent a couple of days trawling the net to find examples of how to do this. I've read much about delimited files, fread functions, etc., but really need a clear dummie's guide of what code to put where... I've tried putting this code into the <head> section of my page... <?php function get_value_of($name) { $lines = file('titles.txt'); foreach (array_values($lines) AS $line) { list($key, $val) = explode('=', trim($line) ); if (trim($key) == $name) { return $val; } } return false; } ?> ...and then this in the <div> tag... <?php echo get_value_of($title1); ?> my txt file is called titles.txt Would really appreciate any help I can get! Thanks in advance.... Scott Quote Link to comment https://forums.phpfreaks.com/topic/204085-loading-variables-from-text-file/ Share on other sites More sharing options...
trq Posted June 7, 2010 Share Posted June 7, 2010 What does titles.txt look like and what output are you after? Quote Link to comment https://forums.phpfreaks.com/topic/204085-loading-variables-from-text-file/#findComment-1068923 Share on other sites More sharing options...
Alex Posted June 7, 2010 Share Posted June 7, 2010 You could use an ini file along with parse_ini_file. Quote Link to comment https://forums.phpfreaks.com/topic/204085-loading-variables-from-text-file/#findComment-1069019 Share on other sites More sharing options...
scottieclark Posted June 7, 2010 Author Share Posted June 7, 2010 hi... thanks for the replies... at the moment the text file looks like this: title1="'self portrait', oil on linen." title2="landscape 1, watercolor on paper." title3="portrait 1, oil on linen." I thought I'd be able to use a function to load variables into an array, then at other points in the page code (html) echo any given variable, importantly, within a <div> tag somewhere in the page layout. Is it possible to put the function code in <head> of an html page, then call for specific variable values to be echoed where I want them? The end goal is to create a site where all the text for each page is loaded in this way. The amount of text involved is minimal so I thought it'd be alright. Any advice? Many thanks for the help! S Quote Link to comment https://forums.phpfreaks.com/topic/204085-loading-variables-from-text-file/#findComment-1069113 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.