FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Going back to a previous line in a CSV file
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Going back to a previous line in a CSV file [message #170143] Tue, 12 October 2010 18:50 Go to next message
pereges is currently offline  pereges
Messages: 4
Registered: September 2010
Karma: 0
Junior Member
Hello, how do I go about doing this.

I've tried using fseek but it doesn't work
Re: Going back to a previous line in a CSV file [message #170144 is a reply to message #170143] Tue, 12 October 2010 19:09 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 12-10-10 20:50, pereges wrote:
> Hello, how do I go about doing this.
>
> I've tried using fseek but it doesn't work
>

i've tried go_back_to_previousline()
but it also did not work....

But using fseek, seems indeed better than go_back_to_previousline()
Can you tell us what u tried with fseek, besides posting here that it
does not work?




--
Luuk
Re: Going back to a previous line in a CSV file [message #170145 is a reply to message #170143] Tue, 12 October 2010 19:20 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(pereges)

> Hello, how do I go about doing this.
>
> I've tried using fseek but it doesn't work

What have you tried so far? What are you trying to accomplish and how do
you load the CSV file? Please some more details and some code.

An additional note:
A CSV is a text file, so using fseek() doesn't make much sense unless
you know exactly where you are in the file and how long your lines are.
It might be better to load the entire file (if it's not too big) into an
array, where you can easily access every single line).

Micha
Re: Going back to a previous line in a CSV file [message #170146 is a reply to message #170145] Tue, 12 October 2010 20:07 Go to previous messageGo to next message
pereges is currently offline  pereges
Messages: 4
Registered: September 2010
Karma: 0
Junior Member
On Oct 12, 3:20 pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(pereges)
>
>> Hello, how do I go about doing this.
>
>> I've tried using fseek but it doesn't work
>
> What have you tried so far? What are you trying to accomplish and how do
> you load the CSV file? Please some more details and some code.
>
> An additional note:
> A CSV is a text file, so using fseek() doesn't make much sense unless
> you know exactly where you are in the file and how long your lines are.
> It might be better to load the entire file (if it's not too big) into an
> array, where you can easily access every single line).
>
> Micha

Hello,

Let me explain what I'm trying to do.

I've a CSV file with the following format:

order_number, item_number
3244343434334, 121212121
3244343434334, 121212122
3244343434334, 121212133
3244343434335, 121212131
3244343434335, 121212132
3244343434335, 121212133
3244343434335, 121212134
............., ................
etc

So the CSV file is sorted in a way that rows with the same
order_number are grouped together as in the above example.

What I need to do is read CSV file line by line, and count the number
of items per order (or per order_number)


Here's the code that I'm using:

<?php
$filename = "test.csv";
$handle_file = fopen($filename,'r'); // open the csv file in read
mode

$counter =0;

////////////Read comma delimited files//////////////
while (($data = fgetcsv($handle_file, 1000, ","))!== FALSE) {
// Skip file heading
if($counter!=0) {
$order_number = (int)$data[0]; // extract the ordernumber
and item number from the current row
$item_number = $data[1];

//now count the number of items per order
$itemcnt = 1; // initialize item count to one

while (($data = fgetcsv($handle_file, 1000, ","))!==
FALSE) { // Keep reading next line until there is a different order
number
$order_num2 = (int)$data[0];
if ($order_number == $order_num2) {
$itemcnt++;
} else {
break;
}

}

echo "Order Number : $order_number " . " ". " Item
Count: $itemcnt" . "<br/>";
} $counter++;
}
?>


When I run this code, certain order numbers were missing like the ones
that are not repeated.
Re: Going back to a previous line in a CSV file [message #170147 is a reply to message #170146] Tue, 12 October 2010 20:12 Go to previous messageGo to next message
pereges is currently offline  pereges
Messages: 4
Registered: September 2010
Karma: 0
Junior Member
I guess what I'm trying to do here is move back and forth between the
file which is so much more difficult with flat files.

I've even considering dumping this CSV data in a mysql table and then
I can process the data easily with query statements.
Re: Going back to a previous line in a CSV file [message #170148 is a reply to message #170147] Tue, 12 October 2010 20:17 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 12-10-10 22:12, pereges wrote:
> I guess what I'm trying to do here is move back and forth between the
> file which is so much more difficult with flat files.
>

reading 'backwords' in a flatfile is indeed 'more' difficult

> I've even considering dumping this CSV data in a mysql table and then
> I can process the data easily with query statements.

this mysql table seems a far better idea...!

--
Luuk
Re: Going back to a previous line in a CSV file [message #170149 is a reply to message #170146] Tue, 12 October 2010 20:41 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(pereges)

> Let me explain what I'm trying to do.
>
> I've a CSV file with the following format:
>
> order_number, item_number
> 3244343434334, 121212121
> 3244343434334, 121212122
> 3244343434334, 121212133
> 3244343434335, 121212131
> 3244343434335, 121212132
> 3244343434335, 121212133
> 3244343434335, 121212134
> ............, ................
> etc
>
> So the CSV file is sorted in a way that rows with the same
> order_number are grouped together as in the above example.
>
> What I need to do is read CSV file line by line, and count the number
> of items per order (or per order_number)

Two questions:

1) What about using a database?
2) How big is the file/how many lines does it contain?

Point 1) would make things _really_ easy and much more flexible, you
should definitely think about it. If that's not possible for some
reason, then you could read the entire file into an array and use PHP's
array functions to work with it. But for doing it this way the file
shouldn't be too big, i.e. not a hundred MB or so.

Given your own code, you don't have to jump back and fourth between the
lines. Just read the file straightaway line by line and use a simple
array for the counts, something like this (untested and without error
checks):

$orders = array();
while (($data = fgetcsv($handle_file, 1000, ",")) !== FALSE) {
$orders[$data[0]]++;
}

That simple. The result will be an array with the order numbers as keys
and the amount of items as values. It will work even if the lines are
not sorted by order number.

But I really suggest to use a database for such stuff.

Micha
Re: Going back to a previous line in a CSV file [message #170160 is a reply to message #170143] Wed, 13 October 2010 13:39 Go to previous messageGo to next message
aaaa is currently offline  aaaa
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
> Hello, how do I go about doing this.
>
> I've tried using fseek but it doesn't work

It does work, but you need offset.
You should use fgetcsv, if you do then:

$f = fopen(...)

while(...)
{
$line = fgetcsv($f);
//going back:
$cur_pos = ftell($f); //remember current pos
fseek($f, $pos, SEEK_SET); //go to previously remembered position
$prev_line = fgetcsv($f); //read line
fseek($f, $cur_pos, SEEK_SET); //go to "current" position
//end of going back
$pos = $cur_pos;
}


--
A
Re: Going back to a previous line in a CSV file [message #170176 is a reply to message #170147] Sat, 16 October 2010 17:52 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
pereges wrote:

> I guess what I'm trying to do here is move back and forth between the
> file which is so much more difficult with flat files.

It is rather easy to do (as long as you are not changing anything) if in the
first pass you store in an array for each line the byte offset that ftell()
tells you, to which you can then fseek() (I am currently doing this to
continue processing a large CSV file after a PHP/DB connection timeout).

Contrary to what Michael Fesser said, this is rather independent of the line
length (RTFM on fgets() and fgetcsv() for details.) There might even be a
built-in or PEAR class for dealing with flat files.

> I've even considering dumping this CSV data in a mysql table and then
> I can process the data easily with query statements.

You could load all the data into an array and work with that probably even
more easily. Look into array_filter(), for example.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to Hide browser status bar Message from client
Next Topic: Mesothelioma
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 00:44:42 GMT 2024

Total time taken to generate the page: 0.02884 seconds