fgetcsv in a table gets extra cell - php

I've used fgetcsv in a table and it works fine except the extra cell at the end.
see http://jobbel.nl/csv.php. How can I fix this?
This is my script:
<table>
<tr>
<th>Datum</th>
<th>Tijd</th>
<th>Temperatuur</th>
</tr>
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<tr><td>";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "</td><td>";
}
echo "</td></tr>";
}
fclose($handle);
}
?>
</table>

You're echoing the cells a bit strange.
Change it to just echo the <td>'s when you actually need them:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<tr>";
$row++;
for ($c=0; $c < $num; $c++) {
echo "<td>" . $data[$c] . "</td>";
}
echo "</tr>";
}
There's an alternative solution, using implode(), that's a bit cleaner:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "<tr><td>" . implode('</td><td>', $data) . "</td></tr>";
}

This should be it...
<?php
$file = file('templog.csv');
foreach($file as $line){
list($datum,$tijd,$temparatuur)=explode(",", $line);
echo "<td>$Date";
echo "<td>$Time";
echo "<td>$Temparature<tr>";
}
?>

Related

How to create dynamic table from both directory and CSV files

I need to create a table whose first column is populated from subdirectory names inside a directory and rest are from a CSV file. This have to be a dynamic table and table headers have to be added from the code. What's wrong with my code?
I am an absolute beginner. So, please ignore my stupidity.
$dir = 'D:\workspace';
$dirlist = preg_grep('/^([^.])/', scandir($dir));
$row = 1;
if (($handle = fopen("D:\workspace\demo\database.csv", "r")) !== FALSE) {
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
foreach ($dirlist as $rowdirectory)
{
echo '<td>' . $rowdirectory . '</td>';
echo '<td>'.$value.'</td>';
}
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
<?php
#------------------------------------------Function for Reading Directory-------------------------------------------
function readdirectory($dir)
{
$dirlist = preg_grep('/^([^.])/', scandir($dir)); // for all(./../anything that
starts with .)
//$dirlist = preg_grep('/[^.]/', scandir($dir)); //only . & ..
//$dirlist =preg_grep('/^[(^.)]/', scandir($dir));//only files that starts with .
return $dirlist;
}
#------------------------------------------Function for Reading CSV file-------------------------------------------
function readcsvfile($source)
{
$handle = fopen($source, "r");
$filecontent = null;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
$filecontent[] = $data;
}
fclose($handle);
return $filecontent;
}
$filecontent= readcsvfile("D:\workspace\demo\database2.csv");
#-------------------Directory Function calling, header array cration and other declaration------------------------------------
$conf_prefix= "../";
$conf_suffix="index.php";
$headerarray= $filecontent[0];
#var_dump($headerarray);
$headersize= count($headerarray);
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
$dir = 'D:\workspace';
$dirlist= readdirectory($dir);
#------------------------------------------Creating 1st row/Header row of Table-------------------------------------------
echo '<tr>';
for($a=0; $a<$headersize;$a++)
{
echo '<td>'.$headerarray[$a].'</td>';
}
echo '</tr>';
#-----------------------------Creating Table elements by comparing both directory arrays and CSV file array-------------------------------------------
foreach ($dirlist as $rowdirectory)
{
foreach ($filecontent as $value) {
$num = count($value);
if ($rowdirectory== $value[0])
{
$link= $conf_prefix. $rowdirectory."/".$conf_suffix;
echo '<tr>';
echo '<td> ' . $rowdirectory . ' </td>';
for( $c=1; $c < $num; $c++) {// loop for csv file
{
echo '<td>'.$value[$c].'</td>';//else print "value"
}
}
echo '</tr>';
}
}
}
?>

php - CSV table with button to move between pages

I need my script will show at the bottom of the table a next and previous page button
any ideas?
The code:
<?php
$row = 1;
if (($handle = fopen("example.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
Any help will be appreciated!................................................

CSV Reader programmin formatting

I am trying to format the csv reader to come out in that way:
1. somestuff, somestuff2, somestuff3
unfortunately the numbers are wrapping up at the top of the table instead of in beginning of the line. any help?
<?php
$row = 1;
$handle = fopen("random.csv", "r");
$number = 1;
echo("<table>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo($number . "." . " " . "<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
$numer ++;
}
echo("</table>");
fclose($handle);
?>
You should create a new cell for those numbers:
<?php
$handle = fopen("random.csv", "r");
$number = 1;
echo '
<table>';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo '
<tr>
<td>' . $number . '.</td>';
foreach ($data as $index => $val) {
echo '
<td>' . $val . '</td>';
}
echo '
</tr>';
$number ++;
}
echo '
</table>';
fclose($handle);

Making HTML friendly data from PHP echo

I've been working with a PHP script to read a contact.csv file. I would like to apply better styling to this table. Is there a way to edit this script to make it more HTML friendly?
$row = 1;
if (($csvFile = fopen("contacts.csv", "r")) !== FALSE) {
$i = 0;
$wantedColumns = array(1,3,57);
echo "<table border='1' cellpadding='0' cellspacing='0'>";
while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
echo "<tr>";
for ($c=0; $c < $num; $c++)
{
if (!in_array($c,$wantedColumns)) continue;
echo "<td>$data[$c]</td>";
}
echo "</tr>";
}
fclose($csvFile);
}
I tried embedding this in html but the echo "<td>$data[$c]</td>"; is throwing it off.
Concatenate all html content in one row and then echo it, otherwise it won't be valid as some tags require the closing </> tag with the opening one <> :
$html = "<table border='1' cellpadding='0' cellspacing='0'>";
while (($data = fgetcsv($csvFile, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
$html .= "<tr>";
for ($c=0; $c < $num; $c++)
{
if (!in_array($c,$wantedColumns)) continue;
$html .= "<td>$data[$c]</td>";
}
$html .= "</tr>";
}
$html .= "</table>";
echo $html;

list through file using a while loop

I cannot get this to work properly, I need the program to list through the records within the file, if $records[$row][2]is the same as the previous, it should repeat class 'field' with a new $records[$row][2], otherwise it should start a new #row. Please help!
if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
$prevRow2 = '';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$records[] = $data;
echo 'Previous'. $prevRow2;
echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
if ($records[$row][2] == $prevRow2) {
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
$prevRow2 = $records[$row][2];
$row++;
}
else {
echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
$prevRow2 = $records[$row][2];
$row++;
echo "</div>";
}
echo "</div>";
}
fclose($handle);
}
First of all, you seem to assume that your CSV file is ordered by the column you want to group on. If this is not the case, then you should first read the file, sort it, and then print it. If your CSV file is not too big for the memory of your server, this should work.
Also, your code has repetitions and store the entire file into an array (actually, just like I suggested above, but I don't know if it is intentional). try something like this:
This is my demo.csv:
"Banana", "yellow"
"Lemon", "yellow"
"Orange", "orange"
"Strawberry", "red"
"Tomato", "red"
This is the PHP to loop through it and list it by color:
<?php
if (($handle = fopen('demo.csv', "r"))) {
$prev = false;
while (($data = fgetcsv($handle, 1000, ","))) {
if ($prev !== $data[1]) {
$prev = $data[1];
echo '<p><b>' . $prev . '</b></p>';
}
echo $data[0] . '<br>';
}
}
fclose($handle);
?>
This is the output in HTML (I added some linebreaks for proper display here)
<p><b>yellow</b></p>
Banana<br>
Lemon<br>
<p><b>orange</b></p>
Orange<br>
<p><b>red</b></p>
Strawberry<br>
Tomato<br>
(If the CSV was not grouped by color, you'd need a different approach.)

Categories