CSV to table problems

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:

name,kills,deaths,company,email af,123131,144141,fdaf,[email protected] afafdsfas,123131,144141,fdaf,[email protected] asdfasf,13,566,sfasdf,[email protected] ggsdfgfds,5678,55,iuluo,[email protected] aruuok,1213,11,uuo98pp0,hyjukee@gagh-obo yjru,44,77,dgwh,jhuytr78jm Balle Klorin,12,1345,ACME,[email protected]

And I have this results.php that includes this:

[code]<?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);
}

?>

HTML Table With PHP <?php foreach ($headers as $header): ?> <?php endforeach; ?> <?php foreach ($data_cells as $data_cell): ?> <?php for ($k = 0; $k < count($headers); $k++): ?> <?php endfor; ?> <?php endforeach; ?>
<?php echo $header; ?>
<?php echo $data_cell[$k]; ?>
[/code]

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 in the attached picture.

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?


Capture2.PNG

Your data seem like valid CSV, it would be easier to use the functions for parsing CSV

Example

[php]<?php
$data = array();
if (($handle = fopen(‘results.csv’, ‘r’)) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ‘,’)) !== FALSE) {
$data[] = $row;
}
fclose($handle);
}

$headers = array_shift($data);
?>

HTML Table With PHP <?php foreach ($headers as $header) { ?> <?php } ?> <?php foreach ($data as $row) { ?> <?php foreach ($row as $column) { ?> <?php } ?> <?php } ?>
<?= $header; ?>
<?= $column ?>
[/php]

Wow, thanks for the quick reply!

I’m gonna try that out later tonight and post my progress here :slight_smile:

I didnt see the stuff about sorting, read up on usort, you should do it after pulling out the headers but before the view :slight_smile:

Added sorting

1 and 2 reflects the columns
name=0, kills=1, etc…

[php]<?php
$fileName = ‘results.csv’;

$ordering = array(
1 => ‘desc’,
2 => ‘asc’
);

$data = array();
if (($handle = fopen($fileName, ‘r’)) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ‘,’)) !== FALSE) {
$data[] = $row;
}
fclose($handle);
}

$headers = array_shift($data);

usort($data, function($a, $b) use ($ordering) {
foreach ($ordering as $key => $sortDirection) {
if (strtolower($a[$key]) > strtolower($b[$key])) {
return $sortDirection == ‘asc’ ? 1 : -1;
} else if ($a[$key] < $b[$key]) {
return $sortDirection == ‘asc’ ? -1 : 1;
}
}
return 0;
});

?>

HTML Table With PHP <?php foreach ($headers as $header) { ?> <?php } ?> <?php foreach ($data as $row) { ?> <?php foreach ($row as $column) { ?> <?php } ?> <?php } ?>
<?= $header; ?>
<?= $column ?>
[/php]

Wow man, just wow! I didn’t expect this kind of help and so fast!

Thank you SO much! Now I know what forum to go to :slight_smile:

It worked exactly as I wanted it to :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service