473,500 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help in retrieving arrays from different file

Hi there

I currently have a .txt file (lets call it jimharper.txt) that holds a
number of arrays like for instance:

$name[] = "Jim Harper"

$relatives[] = "Joan Harper (wife)"
$relatives[] = "Beth Harper (daughter)"

$occupation[] = "Police officer"
$occupation[] = "Teacher"

That I need to be imported into another file (profile.php) so that when
I open profile.php?name=jimharper it extracts the information from the
jimharper.txt file which resides in a different directory.

So the output in the profile.php?name=jimharper would be something like:

Name: Jim Harper
Relatives: Joan Harper (wife), Beth Harper (daughter)
Occupation: Police officer, Teacher

I can figure out how to do it if the arrays were in the same file as the
code but it's not.

I've tried all sorts of things but by now my coding is such a big mess
that I'd rather not share it as it would probably only confuse matters.

Anybody have any ideas?

Kim Jensen

Jul 17 '05 #1
13 1565
Can you just include the txt file in your profile.php? Something like:

$file = "/path/to/file/" . $name . ".txt";
require($file);

Jul 17 '05 #2
rich wrote:
Can you just include the txt file in your profile.php? Something like:

$file = "/path/to/file/" . $name . ".txt";
require($file);


As far as I can see that includes the entire .txt file in the .php page.
I "only" need it to extract the information in the arrays in the .txt
file and sometimes not even all of the arrays. I have two .php files
that extract different (and in some cases some of the same) information
from the .txt file.

Kim Jensen

Jul 17 '05 #3
Ah I didn't realize that would be a problem. The only thing I can
think of, and this is a TOTAL guess, but

Open file
while reading file
if the line starts with a var that i want ($name)
eval() the line

Jul 17 '05 #4
Got it. I made the below just check the first 3 chars of a line... you
can change it to check the line however..

Text file contains:

$foo = "bar";

php file has:

$file = "file.txt";
$fp = fopen($file, "r");
while (!feof($fp)) {
$line = fgets($fp, 1024);
$FirstThree = substr($line, 0, 3);
if ($FirstThree == "\$fo") {
eval($line);
echo $foo;
}
}

The above returns "bar"

Jul 17 '05 #5
rich wrote:
Got it. I made the below just check the first 3 chars of a line... you
can change it to check the line however..
Seems to be working. I don't understand the need for the line-checking
though. Shouldn't it be possible to just ask it to get the $foo-array
once the file has been opened?

Kim Jensen
Text file contains:

$foo = "bar";

php file has:

$file = "file.txt";
$fp = fopen($file, "r");
while (!feof($fp)) {
$line = fgets($fp, 1024);
$FirstThree = substr($line, 0, 3);
if ($FirstThree == "\$fo") {
eval($line);
echo $foo;
}
}

The above returns "bar"


Jul 17 '05 #6
Kim Jensen wrote:
rich wrote:
Got it. I made the below just check the first 3 chars of a line... you
can change it to check the line however..

Seems to be working. I don't understand the need for the line-checking
though. Shouldn't it be possible to just ask it to get the $foo-array
once the file has been opened?

Kim Jensen
Text file contains:

$foo = "bar";

php file has:

$file = "file.txt";
$fp = fopen($file, "r");
while (!feof($fp)) {
$line = fgets($fp, 1024);
$FirstThree = substr($line, 0, 3);
if ($FirstThree == "\$fo") {
eval($line);
echo $foo;
}
}

The above returns "bar"



Kim,

Opening a file does not process it - all it is is a bunch of text. So
foo-array doesn't exist. Rich is correct - you need to eval() the lines
to make it work.

However, I think this would be a lot easier with a database!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #7
Jerry Stuckle wrote:
Opening a file does not process it - all it is is a bunch of text. So
foo-array doesn't exist. Rich is correct - you need to eval() the lines
to make it work.
Ok. I get it.
However, I think this would be a lot easier with a database!


Probably. It's just that I have thousands of files and turning them into
a database would probably take me forever as I don't know of any easy
ways of doing so.

Kim Jensen
Jul 17 '05 #8
Kim Jensen wrote:
Jerry Stuckle wrote:
Opening a file does not process it - all it is is a bunch of text. So
foo-array doesn't exist. Rich is correct - you need to eval() the
lines to make it work.

Ok. I get it.
However, I think this would be a lot easier with a database!

Probably. It's just that I have thousands of files and turning them into
a database would probably take me forever as I don't know of any easy
ways of doing so.

Kim Jensen


Kim,

Yep, it is a chore. But next year you'll have even more files to
convert. And you'll still be spending time manually keying things like
this in, checking for errors, etc.

A database can really help. Perhaps you can automate the processing of
the files with a program?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #9
Jerry Stuckle wrote:
Yep, it is a chore. But next year you'll have even more files to
convert. And you'll still be spending time manually keying things like
this in, checking for errors, etc.
I know. However I have next to no experience with working on databases
but know a bit of php so for now doing it like this seems easier. Right
now my main .php script looks like this:

-----
if ($_GET["name"]) {
$lines = explode("\n",file_get_contents("Who/" .
strtolower($_GET["name"]) . ".txt"));

foreach ($lines as $item) {
if ($item != "") {
$parts = explode("=",trim($item),2);
$item_vars[$parts[0]] = $parts[1];
}
}
-----

and then I retrieve the information using stuff like

echo $item_vars["occupation"]
echo $item_vars["occupation2"]

from a file that has:

occupation=Police officer
occupation2=Teacher

The code end up looking like shit when I have to add for instance up to
echo $item_vars["occupation20"] so I was looking for an easier way to do
it, so that I didn't have to make sure the .php page always had as many
echo's as the .txt pages had $item_vars

I know a database would probably be the best way to get things organized
but until I figure out how to configure one for what I need I just
wanted to clean up my php.
A database can really help. Perhaps you can automate the processing of
the files with a program?


Do you know of any programs that can do that or know where I can look
for some? Considering everything is in .txt files at the moment I would
guess it would be easy turning them files into something that could
easily be converted after a couple of search/replaces or something.

Kim Jensen

Jul 17 '05 #10
Kim Jensen wrote:
Jerry Stuckle wrote:
Yep, it is a chore. But next year you'll have even more files to
convert. And you'll still be spending time manually keying things
like this in, checking for errors, etc.

I know. However I have next to no experience with working on databases
but know a bit of php so for now doing it like this seems easier. Right
now my main .php script looks like this:

-----
if ($_GET["name"]) {
$lines = explode("\n",file_get_contents("Who/" .
strtolower($_GET["name"]) . ".txt"));

foreach ($lines as $item) {
if ($item != "") {
$parts = explode("=",trim($item),2);
$item_vars[$parts[0]] = $parts[1];
}
}
-----

and then I retrieve the information using stuff like

echo $item_vars["occupation"]
echo $item_vars["occupation2"]

from a file that has:

occupation=Police officer
occupation2=Teacher

The code end up looking like shit when I have to add for instance up to
echo $item_vars["occupation20"] so I was looking for an easier way to do
it, so that I didn't have to make sure the .php page always had as many
echo's as the .txt pages had $item_vars

I know a database would probably be the best way to get things organized
but until I figure out how to configure one for what I need I just
wanted to clean up my php.
A database can really help. Perhaps you can automate the processing
of the files with a program?

Do you know of any programs that can do that or know where I can look
for some? Considering everything is in .txt files at the moment I would
guess it would be easy turning them files into something that could
easily be converted after a couple of search/replaces or something.

Kim Jensen


Kim,

Nope, don't know of any programs which can take a custom file such as
yours and convert it. Most programs which do things like you are doing
use databases.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #11
Kim Jensen wrote:
rich wrote:
Can you just include the txt file in your profile.php? Something like:
$file = "/path/to/file/" . $name . ".txt";
require($file);
As far as I can see that includes the entire .txt file in the .php

page. I "only" need it to extract the information in the arrays in the ..txt file

<snip>

If the text file is enclosed with php tags (<?php...?>), the php
parser will parse the included file irrespective of the file extension.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #12
R. Rajesh Jeba Anbiah wrote:
Kim Jensen wrote:
rich wrote:
Can you just include the txt file in your profile.php? Something
like:
$file = "/path/to/file/" . $name . ".txt";
require($file);


As far as I can see that includes the entire .txt file in the .php


page.
I "only" need it to extract the information in the arrays in the


.txt
file


<snip>

If the text file is enclosed with php tags (<?php...?>), the php
parser will parse the included file irrespective of the file extension.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/


Not unless you open it through Apache AND have the file extension set up
in Apache to handle PHP.

For instance - placed in a .html file, PHP code will NOT be parsed (even
if it's withint <? and ?>) unless you tell Apache to parse .html files
as PHP.

And they will NEVER be parsed if you open them with fopen() - as was
recommended here.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #13
Jerry Stuckle wrote:
R. Rajesh Jeba Anbiah wrote:
Can you just include the txt file in your profile.php? Something
like:
$file = "/path/to/file/" . $name . ".txt";
require($file);

As far as I can see that includes the entire .txt file in the .php page. I "only" need it to extract the information in the arrays in the ..txtfile

<snip>
If the text file is enclosed with php tags (<?php...?>), the php
parser will parse the included file irrespective of the file

extension.
Not unless you open it through Apache AND have the file extension set up in Apache to handle PHP.

For instance - placed in a .html file, PHP code will NOT be parsed (even if it's withint <? and ?>) unless you tell Apache to parse .html files as PHP.

And they will NEVER be parsed if you open them with fopen() - as was
recommended here.


As you see, the context of discussion is about require/include .txt
file in .php page. And your comment is hardly relevant here.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3440
by: Dariusz | last post by:
I want to use arrays in my website (flat file for a guestbook), but despite having read through countless online tutorials on the topic, I just can't get my code to work. I know there are...
31
14284
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
8
5448
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
2800
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
1
1487
by: bobmct | last post by:
Fellow PHP'ers; I'm digging myself a hole on this one so I thought it has come to the time when I must ask those who know. I have what should be a simple question for loading, accessing and...
16
2768
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
15
2550
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
9
2479
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
4
2238
by: ndedhia1 | last post by:
I am reading in a file to see what delays I am getting on what IP address: QuoteBlockTiming exceeded 1000 ms: 1684 --- Fri Nov 06 06:09:10 CST 2009 170.137.94.95 Class key = 649126730 block...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.