Skip to content

Instantly share code, notes, and snippets.

@marcaum54
Last active August 29, 2015 13:56
Show Gist options
  • Save marcaum54/9243123 to your computer and use it in GitHub Desktop.
Save marcaum54/9243123 to your computer and use it in GitHub Desktop.
<?php
$pathCsv = __DIR__ . '/arquivos/';
$arquivos = array(
'contas1.csv',
'contas2.csv'
);
$rows = array();
foreach($arquivos as $arquivo)
{
if(($handle = fopen($pathCsv . $arquivo, "r")) !== false)
{
$firstRow = null;
while(($data = fgetcsv($handle, 1000, ",")) !== false)
{
for($c = 0; $c < count($data); $c++)
{
if(!$firstRow)
$firstRow = $data[$c];
else
$rows[$arquivo][] = array_combine(explode(';', $firstRow), explode(';', $data[$c]));
}
}
fclose($handle);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Banco de dados 2 - Leitura de arquivo CSV</title>
<link rel="icon" href="http://www.fa7.edu.br/icon_fa7.gif" type="image/gif" />
<link rel="stylesheet" type="text/css" href="public/bootstrap/css/bootstrap.min.css" media="screen">
</head>
<body>
<div class="container" style="padding-top: 20px;">
<h1>Banco de dados 2 - Leitura de arquivo CSV</h1>
<h4>Código fonte - <a href="https://gist.github.com/marcaum54/9243123" target="_blank">clique aqui</a></h4>
<div class="row center-block">
<?php if($rows): ?>
<?php foreach($rows as $arquivo => $arquivoRows): $total = 0; ?>
<div class="well" style="background-color: #FFF;">
<h2><?php echo $arquivo; ?></h2>
<table class="table table-striped">
<thead>
<tr>
<?php foreach(array_keys($arquivoRows[0]) as $colTitle): ?>
<th><?php echo $colTitle; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($arquivoRows as $arquivoCols): $total += $arquivoCols['saldo']; ?>
<tr>
<?php foreach($arquivoCols as $value): ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h1 style="color: red;">Saldo Total: <?php echo $total; ?></h1>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment