Jump to content

Archived

This topic is now archived and is closed to further replies.

RabPHP

Counting for Data Entry

Recommended Posts

Greetings all,

I have an application which reads and write text files.  When reading, my data looks like this...


if (file_exists($filename)) {
$handle = fopen("$filename", "r");
$contents = fread($handle, 10240);
$data = explode("|", $contents);

$somename = "$data[0]";
$someaddress = "$data[1]";
$somephone = "$data[2]";
and on and on. 

fclose($handle);
}


Some of the data has 300 columns.  My issue is that every time I make a change I have to renumber the whole file.  I believe I can do some sort of count++ but I am not that well versed in that function.  If anyone can provide assistance please let me know, thanks!

Rab

Share this post


Link to post
Share on other sites
At first, using just file() is easier than all that opening, reading, and exploding...

$data = file("$filename");

is almost the same as

$handle = fopen("$filename", "r");
$contents = fread($handle, 10240);
$data = explode("|", $contents);
fclose($handle);

But I don't quite understand what you mean by renumbering the file?

Share this post


Link to post
Share on other sites
In the text file each entry is seperated with a Pipe.  Rather then numbering each return in the array with [0], [1], [2], [4], [5] etc I would like to have something that would count each line, so my code would look something like...

$somename = "$data[$i]";//Where $i is incrementally increased by 1 for each variable.
$someaddress = "$data[$i]";
$somephone = "$data[$i]";

Rab

Share this post


Link to post
Share on other sites
How does the $somename, $someaddress, $somephone continue?  :-\

Share this post


Link to post
Share on other sites
The data looks like this...

JohnDoe|123 Memory Lane|555-1212|etc all seperated with a pipe in a flat file.  There may be 200-300 pieces of data per file.  When I am pulling that information out I am setting the variable names to equal a specifc place in the array.  I just don't want to have to number each element in the array every time I make a change.  I will have to change the variable names though, thats a given.  I just want the number in the [] to count up.

Rab

Share this post


Link to post
Share on other sites
so what you're saying is that if you change JohnDoe|123 Memory Lane| to John|Doe|123 Memory Lane|, you'd have to change $somename to $data[0].$data[1]? and you want to avoid that?

Share this post


Link to post
Share on other sites
Sounds like I'm not expressing what I need correctly.

For each item between the deliminator I have an value.  I want to keep the value exactly as it is in the data.  When I am readint that data, I read it like this...

$somename = "$data[$0]";
$someaddress = "$data[1]";
$somephone = "$data[2]";
$somecity = "$data[$3]";
$somestate = "$data[4]";
$somezip = "$data[5]";
...
...
$info = "$data[200];

and on and on.  Some of these data file have 300 pieces of information.  If I change the data written so that I insert something at the beginning, I have to renumber all the lines so the variable are set to the right place in the array.  For example if i add a suffix later it would look like...

$suffix =  = "$data[$0]";
$somename = "$data[$1]";
$someaddress = "$data[2]";
$somephone = "$data[3]";
$somecity = "$data[$4]";
$somestate = "$data[5]";
$somezip = "$data[6]";

$somename now becomes position 1.  I would like it so that when I make a change I don't have to renumber hundreds of lines so that the variables are set to the correct place in the data file.

Rab

Share this post


Link to post
Share on other sites
Well...
$count = 0;
$somename = $data[$count]; $count++;
$someaddress = $data[$count]; $count++;
$somephone = $data[$count]; $count++;
$somecity = $data[$count]; $count++;
$somestate = $data[$count]; $count++;
$somezip = $data[$count]; $count++;
...
...
$info = $data[$count];

Is one way...

Share this post


Link to post
Share on other sites
or shorter
[code]$i=0;
$someone = $data[$i++];
$someaddress = $data[$i++];
...[/code]

Share this post


Link to post
Share on other sites
or shorter  ;D

Btw, the first key must be just $i, otherwise it would start from 1

Share this post


Link to post
Share on other sites
I was thinking... Will it increase the $i?
using
$someone = $data[$i+1];
$someaddress = $data[$i+1];
will return 1 on both of them I think... And  ++ is the same as +1?

Share this post


Link to post
Share on other sites
You probably want to define an array at the start of your script that has the variable names in the same order as the entries in the file, so you would do something like:
[code]<?php
$vars = array('somename','someaddress','somephone','somecity'); // .... etc
$fp = fopen($filename,'r');
while (($data = fgetcsv($fp,10240,'|')) !== false) { // fgetcsv will automagically do the explode
    for ($i=0;$i<count($data);$i++)
          ${$vars[$i]} = $data[$i];  // using variable variables here
//
//  the rest of your code
//
}
?>[/code]
When you add a new column, you just have to change the array of variables.

Ken

Share this post


Link to post
Share on other sites
Thanks!  Both of those suggestions work. For Ifa, the first i++ seems to start at 0 even though you already set it to 0 before hand.  I will convert it to kenrbnsn's last suggestion once I get this first release done.

Thanks again guys!

Rab

Share this post


Link to post
Share on other sites
Ifa, $i++ means increment $i by 1 after the call is completed, so it is still 0 when $data[$i++] is called.

$i++ actuall means $i = $i+1 after the line.

Share this post


Link to post
Share on other sites

×

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.