Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/204085-loading-variables-from-text-file/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.