Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

[Help] CSV to table problems

General discussions related to php

Moderators: macek, egami, gesf

[Help] CSV to table problems

Postby myggstikk » Tue Feb 11, 2014 4:17 am


My experience with php is limited, but I'm working on a tiny project that I feel so close to achieving, but I just can't get it to work properly.

I have this results.csv that has let's say these lines:
Code: Select all
Balle Klorin,12,1345,ACME,

And I have this results.php that includes this:
Code: Select all
$inputfile = file("results.csv");

$data_lines = array();
foreach ($inputfile as $line)
    $data_lines[] = explode("\r\n", $line);

//Get column headers.
$first_line = array();
foreach ($data_lines[0] as $dl)
    $first_line[] = explode(",", $dl);

$headers = array();
foreach ($first_line as $fl)
    $headers = $fl;

// Get row content.
$data_cells = array();
for ($i = 1; $i < count($data_lines); $i++)
    $data_cell = array();
    for ($j = 0; $j < count($headers); $j++)
        $data_cell[$j] = substr($data_lines[$i][$j], strpos($data_lines[$i][$j], ","));
    $data_cells[$i] = $data_cell;

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>HTML Table With PHP</title>
        <table border="1">
            <?php foreach ($headers as $header): ?>
                <th><?php echo $header; ?></th>
            <?php endforeach; ?>
        <?php foreach ($data_cells as $data_cell): ?>
            <?php for ($k = 0; $k < count($headers); $k++): ?>
                <td><?php echo $data_cell[$k]; ?></td>
            <?php endfor; ?>
        <?php endforeach; ?>

Now, what I'm trying to do is show this csv nicely in an html table. For some reason I can't get the cells into the correct cells, they all end up like this:
Capture2.PNG (12.27 KiB) Viewed 132 times

The entries from the name-column are gone and it's all in one cell... Can anyone help me here?

I also want this sorted by highest kills first, then by lowest deaths, but that will probably be quite easy once I get this fixed...

I've been googling for days and trying all sorts of methods to do this, but this is actually the closest I've come to what I want to accomplish. Can some of you please push me in the right direction here?
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Feb 11, 2014 4:08 am

Re: [Help] CSV to table problems

Postby myggstikk » Wed Feb 12, 2014 1:48 am

I got this answered in another forum, so no need to answer this, it works now :)
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Feb 11, 2014 4:08 am

Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.