Page 1 of 1

(Solved) Please help: populate array() from text file?

Posted: Wed Aug 03, 2005 11:11 am
by DeprecatedDiva
Hi everyone. This is my first post. I am two weeks old in my exposure to & learning PHP and I am absolutely excited at the possibilities!

However, I've run into a problem. Well, not really a problem. Just a question of efficiency.

Background history: I have a customer who wants to minimize - AS MUCH AS POSSIBLE! - the risk of inadvertently screwing up the code that builds her pages. So, in light of that, I wanted to be able to populate the array from an external text file instead of having to prepopulate the field thus:

Code: Select all

$filename = array('index','contact','thisfile','thatfile',);
She would be the one building the text file where the input would simply be something along the lines of a shopping list:

Code: Select all

index
contact
thisfile
thatfile
and have that file populate the array.

Rather than bug folks, I've searched, googled, and re-searched until I am ready to throw in the towel. I am probably not using the correct search parameters or understanding the material I've read.

Is there somewhere a prepackaged code that I can follow until I gain more experience coding PHP myself?

Thank you in advance.

Posted: Wed Aug 03, 2005 11:13 am
by timvw
I think you want to have a look at http://www.php.net/file.


In our starterpack threads you will find loads of hints where you can find the information you need...

Posted: Wed Aug 03, 2005 12:14 pm
by josh
Hint: split();

Extra huge hint: \n marks a new line, but so does \r\n on windows so you'll need split() to check for \n \r\n and \r for newlines (\r on mac me thinks)

Posted: Wed Aug 03, 2005 12:31 pm
by DeprecatedDiva
:D Thanks for the tips guys! Now I gotta go read and learn....

Of course after posting this, I thought of another search and found the same information you folks have pointed me toward :oops: :D

I'm sure I will be back with more questions. Ciao for now!!!

Posted: Sun Oct 23, 2005 1:08 pm
by DeprecatedDiva
Okay guys, I've played with file() and it does as advertised. I've also played with split() and it does as advertised as well. But for the life of me, I just can't figure out how to get this:

Code: Select all

$textfile = 'includes/pagelist.txt';
$pagename = file($textfile) or die('Could not read file!');
where the contents of pagelist.txt is:
  • home
    contact
    thisfile
    thatfile
converted into this:

Code: Select all

$pagename = array('home','contact','thisfile','thatfile');
so I can use this:(code from http://digital-web.com/articles/easypeasy_php_2/)

Code: Select all

// 1. Define an array of allowed $_GET values:
    $pagename = array('home','contact','thisfile','thatfile');
// 2. If there is no $_GET['id'] defined, then serve the homepage:
    if (!isset($_GET['id'])) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/includes/home.inc');
    }
// 3. If the page is allowed, include it:
    elseif (in_array($_GET['id'], $pagename)) {
    include ($_SERVER['DOCUMENT_ROOT'] . '/includes/' . $_GET['id'] . '.inc');
    }
// 4. If the page is not allowed, send them to an error page:
    else {
    header("HTTP/1.0 404 Not Found");
    include ($_SERVER['DOCUMENT_ROOT'] . '/includes/error.inc');
}
I just can't seem to wrap my mind around the necessary steps. :cry:

If you have any further examples or lessons that may help me, I would greatly appreciate a pointer in the right direction.

Posted: Sun Oct 23, 2005 4:25 pm
by feyd

Code: Select all

$lines = file('filename.txt');
$pagename = array_map('trim',$lines);
var_export($pagename);

Posted: Sun Oct 23, 2005 5:44 pm
by DeprecatedDiva
feyd, BLESS YOU! I looked at array_map earlier but didn't understand it would serve my needs. (I need a dunce cap....) It works like a champ! Now pages can be added as needed without having to modify the master files.

I think it's time to start from page one of the "book" and start playing with the examples....