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

Hello,

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
name,kills,deaths,company,email
af,123131,144141,fdaf,asdfdsf@fsg.no
afafdsfas,123131,144141,fdaf,asdfdsf@fsg.no
asdfasf,13,566,sfasdf,aag@gsg.no
ggsdfgfds,5678,55,iuluo,fjfo@popo.vom
aruuok,1213,11,uuo98pp0,hyjukee@gagh-obo
yjru,44,77,dgwh,jhuytr78jm
Balle Klorin,12,1345,ACME,balleklor@acme.com


And I have this results.php that includes this:
Code: Select all
<?php
$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;
    unset($data_cell);
}

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


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
Capture2.PNG (12.27 KiB) Viewed 109 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?
myggstikk
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 :)
myggstikk
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.

cron