View Single Post
  #4 (permalink)  
Old 12-30-03, 04:12 PM
lordmerlin lordmerlin is offline
Newbie Coder
 
Join Date: Jul 2003
Location: South Africa
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
This is a great help, thanx.

Just one last one to it. How do I access the values the amount of columns change, I won't be able to access them like this:

PHP Code:

print $a_columns[1]; print $a_columns[2]; 

What do I need to todo to access anyone of these?
From here I would need to insert the values into a database. But I need to to check if the amount of columns equal the amount of columns in the database, and if the column names are the same. They have a format of
Block 1FE; Block 1 NFE; BLock 2FE; Block 2NFE, and so on, until the last one that was taken, be it say Block 25NFE.

The main problem is, is a reading was skipped, then it shouldn't be able to insert the code into the table.

tia

Quote:
Originally Posted by blaw
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,
Reply With Quote