Forum Moderators: coopster

Message Too Old, No Replies

fgetcsv - retain value for next line if empty

         

discord

7:10 pm on Nov 10, 2010 (gmt 0)

10+ Year Member



I have a csv file in this format:

004AA744D82D64CA86257753007134CF
"","10/06/2010 01:10:30 PM","Created Payment Receipt","","",""
"","10/06/2010 01:10:26 PM","$ Fee Payment Recorded","","",""
"","09/28/2010 04:47:08 PM","Created Email to Client","","",""
008077543BBBCF588625770300748FE0
"","09/24/2010 04:11:58 PM","Created Letter - Appearance Letter","","",""
"","08/30/2010 01:14:52 PM","Created Payment Recept","","",""

Where hex value is the ID for the lines that follow. When a new hex value appears, it serves as the ID for the next few lines, etc.

I am having trouble getting the hex value to be constant until the next value is reached.

I have tried the following, and I know why it doesn't work, I am just too new to php to know where to go from here.

while (($data = fgetcsv($handle, 0, ',', '"'))) {
$hexID = clean($data[0]);
if (empty($data[0])){
$hexID = $hexID;
}
if (empty($data[1])){
$date = '';
}else{
$tmpdate = clean($data[1]);
$exp = explode(' ', $tmpdate);
$time = $exp[1];
$expdate = explode ('/', $exp[0]);
$glued = $expdate[2].'-'.$expdate[0].'-'.$expdate[1];
$date = $glued.' '.$time;
}
if (empty($data[2])){
$text = '';
$text = $data[2];
}

*edited for grammar

andrewsmd

10:27 pm on Nov 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't really understand what you are asking. Post some data and some pseudo or explanation of what you want to do with it.

discord

10:56 pm on Nov 10, 2010 (gmt 0)

10+ Year Member



I must have been unclear, here is what I am trying to do.

Open a csv file, read line by line. The first line has one value, a hex id. all subsequent lines are data to be associated with that value, until the next hex is reached, then all data under that one will be associated to that new hex value. So I want to use the hex as the first field value in the next few lines until a new hex id is reached.

Thank you

discord

12:21 am on Nov 12, 2010 (gmt 0)

10+ Year Member



I got it. Just a simple switcharoo was needed.

while (($data = fgetcsv($handle, 0, ',', '"'))) {
if (!empty($data[0])){
$hexID = clean($data[0]);
}