Jump to content


Photo

Counting for Data Entry


  • Please log in to reply
14 replies to this topic

#1 RabPHP

RabPHP
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 02 August 2006 - 03:55 PM

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

#2 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 02 August 2006 - 04:08 PM

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?

I'm just a guy who likes to code for fun...

#3 RabPHP

RabPHP
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 02 August 2006 - 04:16 PM

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

#4 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 02 August 2006 - 04:21 PM

How does the $somename, $someaddress, $somephone continue?  :-\
I'm just a guy who likes to code for fun...

#5 RabPHP

RabPHP
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 02 August 2006 - 04:31 PM

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

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 04:39 PM

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?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 RabPHP

RabPHP
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 02 August 2006 - 04:49 PM

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

#8 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 02 August 2006 - 04:56 PM

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...

I'm just a guy who likes to code for fun...

#9 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 05:15 PM

or shorter
$i=0;
$someone = $data[$i++];
$someaddress = $data[$i++];
...

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#10 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 02 August 2006 - 05:21 PM

or shorter  ;D

Btw, the first key must be just $i, otherwise it would start from 1
I'm just a guy who likes to code for fun...

#11 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 02 August 2006 - 05:25 PM

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?
I'm just a guy who likes to code for fun...

#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 August 2006 - 05:30 PM

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:
<?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
//
}
?>
When you add a new column, you just have to change the array of variables.

Ken

#13 RabPHP

RabPHP
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 02 August 2006 - 05:37 PM

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

#14 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 06:06 PM

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#15 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 02 August 2006 - 06:16 PM

cool, thanks
I'm just a guy who likes to code for fun...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users