View Single Post
  #3 (permalink)  
Old 12-29-03, 06:07 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

I guess tia meant something like this:

Code:
Amy,1980-01-26,604-111-2222,Vancouver
Bob,1979-06-25,604-222-3333,Montreal
Cathy,1967-11-21,250-333-4444,Toronto
This is a CSV file, having 4 columns and 3 rows.

Assuming what tia meant was that, though this time there are 4 colmns, there might be 5, 6, or 28 columns next time, and wants to have as many <td></td>'s as there are columns in the CSV file.

If that's the case, you can explode the first line into an array to get the column count (please read the comment as it may be helpful for you to understand what's going on):

PHP Code:

<?php


// Get the file contents into an array. file() takes each line of the specified file and assign it to an array value. You will get an array var $a_lines.
$a_lines file('/path/to/yourfile.csv');

// Get one line from the array for explosion.
// explode() takes a string and splits it into array values, delimited by the delimiteer of your choice - it's the comma here.
$a_columns explode(','$a_lines[0]);

// Now count how many "columns" are in there.
$num_columns count($a_columns);

// Just to make sure, fire it off here.
// You should get 4 when tried with the above CSV file.
echo $num_columns;

?>
It seemed that you know how to handle once you have the column number, so I'll cut it here. Hope this is what you wanted.

Good night,
__________________
Blavv =|
Reply With Quote