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 #170146 is a reply to message #170145] Tue, 12 October 2010 20:07 Go to previous messageGo to previous message
pereges is currently offline  pereges
Messages: 4
Registered: September 2010
Karma:
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.
[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: Tue Nov 26 23:44:28 GMT 2024

Total time taken to generate the page: 0.04732 seconds