Jump to content

Counting for Data Entry


RabPHP

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
Link to comment
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?
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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...
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

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.