Link to home
Create AccountLog in
Avatar of maccaj51
maccaj51Flag for Afghanistan

asked on

CSV and Row Splitting

Hi Experts,

I have been on this for about 24 hours now and not getting anywhere.

I need to import a CSV file...
Split it into rows...
Split it into columns...
Apply a different html format to the first row...

Could some please help!!!

Many Thanks
Avatar of Php_expert
Php_expert
Flag of India image

Avatar of Julian Hansen
Take a look at the php fgetcsv function

http://www.php.net/manual/en/function.fgetcsv.php

If you would like some code post more info about what you are trying to do.
Avatar of maccaj51

ASKER

Hi Julian,

Thats what ive been working from...

Im trying to take a csv file... and put it into this format... By:

1) Splitting it into rows
2) Putting first row in thead
3) Putting remaining rows in tbody

Any Ideas? Ive been miserably unsuccessful!

<table>
    <thead>
      <tr>
        <th>Title1</th>
        <th>Title2</th>
        <th>Title3</th>
        <th>Title4</th>
        <th>Title5</th>
        </tr>
      </thead>
    <tbody>
      <tr>
        <td>Item1a</td>
        <td>Item1b</td>
        <td>Item1c</td>
        <td>Item1d</td>
        <td>Item1e</td>
        </tr>
      <tr>
        <td>Item2a</td>
        <td>Item2b</td>
        <td>Item2c</td>
        <td>Item2d</td>
        <td>Item2e</td>
        </tr>
    </tbody>
</table>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi Ray,

MANY MANY thanks for that.

However they all output as <th>'s...

It cant distinguish between rows?
Uhh, I think only the top row is <th>, right?
Correct...
My thinking is along these lines:
http://www.w3schools.com/tags/tag_th.asp

Does that make sense to you, or am I missing some part of the question?
Is it possible to set an integer to break each line off at?
Thats exactly right in your code the first line is <th> and the rest of the lines are <td>...

However it only outputs <th> from my csv file...

Suggesting it cant recognise where each row starts and ends?
Hero... just needed :
ini_set("auto_detect_line_endings", true);
Interesting.  I do not have that setting on in my server, but it seems to work just fine.   Maybe it has something to do with the differences in EOL characters between operating systems.  You might want to try reading with file() also to see how that works.

Anyway, it's a good question.  Thanks for the points, ~Ray