RabPHP Posted August 2, 2006 Share Posted August 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
Ifa Posted August 2, 2006 Share Posted August 2, 2006 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? Quote Link to comment Share on other sites More sharing options...
RabPHP Posted August 2, 2006 Author Share Posted August 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
Ifa Posted August 2, 2006 Share Posted August 2, 2006 How does the $somename, $someaddress, $somephone continue? :-\ Quote Link to comment Share on other sites More sharing options...
RabPHP Posted August 2, 2006 Author Share Posted August 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 2, 2006 Share Posted August 2, 2006 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? Quote Link to comment Share on other sites More sharing options...
RabPHP Posted August 2, 2006 Author Share Posted August 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
Ifa Posted August 2, 2006 Share Posted August 2, 2006 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... Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 2, 2006 Share Posted August 2, 2006 or shorter[code]$i=0;$someone = $data[$i++];$someaddress = $data[$i++];...[/code] Quote Link to comment Share on other sites More sharing options...
Ifa Posted August 2, 2006 Share Posted August 2, 2006 or shorter ;DBtw, the first key must be just $i, otherwise it would start from 1 Quote Link to comment Share on other sites More sharing options...
Ifa Posted August 2, 2006 Share Posted August 2, 2006 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? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 2, 2006 Share Posted August 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
RabPHP Posted August 2, 2006 Author Share Posted August 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted August 2, 2006 Share Posted August 2, 2006 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. Quote Link to comment Share on other sites More sharing options...
Ifa Posted August 2, 2006 Share Posted August 2, 2006 cool, thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.