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
Return to the default flat view Create a new topic Submit Reply
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 previous message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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 Oct 19 20:37:04 GMT 2024

Total time taken to generate the page: 0.03850 seconds