Problems with using PHP to display CSV file as HTML table - php

I'm trying to display a two column, four row CSV file in an HTML table. I have the code below, but it only displays the first row and I don't understand why.
<html>
<head>
<title>test</title>
</head>
<body>
<table border=1>
<?PHP
$file_handle = fopen("oee1.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td></tr>';
}
fclose($file_handle);
?>
</table>
</body>
</html>
The csv file looks like:
test1, 1
test2, 2
test3, 3
test4, 4

You were not iterating through every line.
Try something like this:
<!DOCTYPE html>
<html>
<head>
<title>+test</title>
</head>
<body>
<?php
if (($file_handle = fopen("data.csv", "r")) !== false) {
$str = '';
$str .= '<table>';
while (($data = fgetcsv($file_handle, 1024, ",")) !== false) {
$str .= '<tr>';
foreach ($data as $key => &$value) {
$str .= "<td>$value</td>";
}
$str .= '</tr>';
}
fclose($file_handle);
$str .= '</table>';
echo $str;
}
?>
</body>
</html>
output:
<table>
<tbody>
<tr>
<td>test1</td>
<td>1</td>
</tr>
<tr>
<td>test2</td>
<td>2</td>
</tr>
<tr>
<td>test3</td>
<td>3</td>
</tr>
<tr>
<td>test4</td>
<td>4</td>
</tr>
</tbody>
</table>
PS: make sure you have priviledges and your csv is in the smae directory as your php file.
Reference : fgetcsv

try adding a call to fgets($file_handle); in each iteration of the loop, you have to advance the file handle pointer somehow.

Related

How to print a table by given row index and column index put the Line value in table Column value in json data in php

i am trying to print a table from json data which contain row index and column index my array is given below
[{"ID":["64103487-ab83-4ce2-ba22-e4c83184eeda"],"RowIndex":1,"ColumnIndex":1,"Line":""},{"ID":["a38cb3ca-9565-463c-830d-f686cb2a965e"],"RowIndex":1,"ColumnIndex":2,"Line":""},{"ID":["73863297-6182-4c24-a5cc-13a20f8b9b97"],"RowIndex":1,"ColumnIndex":3,"Line":"Department"},{"ID":["6c87f8d1-e7b5-49e6-8d45-049532177b0e"],"RowIndex":2,"ColumnIndex":1,"Line":""},{"ID":["ef36343e-e89a-4671-9672-310fdd451111","5e210db5-979e-4b88-9ac1-8d7d3776569c"],"RowIndex":2,"ColumnIndex":2,"Line":""},{"ID":["301021a5-9a2b-4147-85f1-def1b91c0543"],"RowIndex":2,"ColumnIndex":3,"Line":"Sales"},{"ID":["503339ef-2892-460d-9638-d08a6ebeaf5a"],"RowIndex":3,"ColumnIndex":1,"Line":"3"},{"ID":["8fff1706-8091-4ee4-9515-627e68758109","98d44dc1-a620-4de9-9eb0-4af878a46b6f"],"RowIndex":3,"ColumnIndex":2,"Line":"Justin Shore"},{"ID":["07172dfe-2037-4355-87c8-03fef616634c"],"RowIndex":3,"ColumnIndex":3,"Line":"Marketing"},{"ID":["c88a583d-f5c8-48fb-aaa8-8a1bda6bf1d5"],"RowIndex":4,"ColumnIndex":1,"Line":"4"},{"ID":["30696ecb-aabc-4735-bdf8-47f864260e26","4179cd2e-8132-4baa-afec-41d4b33870a3"],"RowIndex":4,"ColumnIndex":2,"Line":"Lexi Barringer"},{"ID":["a67c9508-7731-4953-b88e-df7dd58c6c34"],"RowIndex":4,"ColumnIndex":3,"Line":"Finance"},{"ID":["153f68af-a9aa-49a9-ab26-ad9f89e21ecc"],"RowIndex":5,"ColumnIndex":1,"Line":"5"},{"ID":["0916eb3e-e8e0-43fd-a424-bada4c95080b","66dd35de-f4d7-48e3-bb4a-a42e158c28c7"],"RowIndex":5,"ColumnIndex":2,"Line":"Safa thsan"},{"ID":["2aa280ff-b527-4b4b-9ca5-3538a3fcbb98"],"RowIndex":5,"ColumnIndex":3,"Line":"Sales"},{"ID":["3d9af0ca-e0e8-4747-bfc7-85273a053a0f"],"RowIndex":6,"ColumnIndex":1,"Line":"6"},{"ID":["99314a19-0377-416d-bc25-6efcf4943abb","d001dfb2-d51c-460d-bbd7-d2340695dddb"],"RowIndex":6,"ColumnIndex":2,"Line":"Roy Flint"},{"ID":["66201bf3-aa64-4761-a291-040310b1b5cb"],"RowIndex":6,"ColumnIndex":3,"Line":"Finance"}]
and i want to ouput like the image given below
enter image description here
<?php
$data = file_get_contents('test.json');
$data =json_decode($data,TRUE);
$dataA= array();
foreach($data as $singleArray){
array_push($dataA, $singleArray['Line']);
}
$arrayChunks= array_chunk($dataA, 3);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<thead>
<th>Names</th>
<th>Department</th>
</thead>
<tbody>
<?php
foreach($arrayChunks as $array){
if(empty($array[0]) || empty($array[1]) || empty($array[2]))continue;
?>
<tr>
<td><?php echo $array[1]; ?></td>
<td><?php echo $array[2]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
Hi Its exactly what you are looking. try to check this and setup output as you need. Its just idea you can surely improve your problem using this. Thanks
This is what i want
result as image given in question
$data = json_decode($data , true);
$k = 1;
$l=1;
$html .='<table class="table table-striped table-bordered" border="1">';
foreach($data as $combine){
if($combine['RowIndex']==$k){
$html .='<tr>';
foreach($data as $com){
if($com['RowIndex']==$l){
$html .='<td>'.$com['Line'].'</td>';
}
}
$html .='</tr>';
//$k=$combine['RowIndex'];
$k++;
$l++;
}
}
$html .='</table>';
print_r($html);
die;

Proper displaying of csv file data in table if some fields are empty

I want to display a .csv file neatly in a table using php. Some content of the file are empty field.
Here is my first approach, got an error but displaying the datas in one row. Supposedly they should display in their respectively fields.
The code output here:
Should be like this:
<table border = "1">
<tr>
<th>NAME</th>
<th>Email</th>
<th>Address</th>
<th>Payment</th>
<th>Datepaid</th>
</tr>
<?php
$data = file("data/payment.csv");
foreach ($data as $line){
$lineofarray = explode("\t", $line);
list($name, $email, $address, $payment, $datepaid) = $lineofarray;//error here
?>
<tr>
<td>
<?= $name?>
</td>
<td>
<?= $email?>
</td>
<td>
<?= $address?>
</td>
<td>
<?= $payment?>
</td>
<td>
<?= $datepaid?>
</td>
</tr>
<? }?>
</table>
My code now had an error on the part of list. And displaying the datas in one field. Hope you can help me. Thanks
Please try this.
$fp = fopen("data/payment.csv", "r");
while (($line = fgetcsv($fp)) !== false) {
echo "<tr>";
foreach ($line as $field) {
echo "<td>" . htmlspecialchars($field) . "</td>";
}
echo "</tr>\n";
}
This is a safe way to read csv lines as it is unicode safe and also supports fields that are wrapped in double quotes.
You should use fgetcsv:
$fp = #fopen('data/payment.csv', 'r');
$tbl = '<table><thead><tr><th>NAME</th><th>Email</th><th>Address</th><th>Payment</th><th>Datepaid</th></tr></thead><tbody>';
while($r = fgetcsv($fp)){
$tbl .= '<tr>';
foreach($r as $v){
$tbl .= '<td>'.htmlentities($v, ENT_QUOTES, 'UTF-8').'</td>';
}
$tbl .= '</tr>';
}
$tbl .= '</tbody></table>';
echo $tbl; // echo $tbl wherever you want

Changing column in HTML table to hyperlink

for ($i=0; $i<=$lines; $i++)
{
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
This is my code, it read's from a text file and explode in table, but I have a little problem here cause this one needs to be link.
<td width="20%">'.$part[2].'</td>
.$part[2]. is just a word from file but it has query like www.somesite.com/?q= There at the end I need to have that
word from file
that kind of code did not work for me
<td width="20%"> <a herf='www.somesite.com/?q=''.$part[2].'> '.$part[2].' </a> </td>
I realy need some help with this...
<?php
//first, get the file...
$file = file('req.txt');
//now count the lines ..
$lines = count($file);
//start the table here..
echo'<table border="2" width="100%">';
echo'<tr>
<td width="20%">Naslov</td>
<td width="20%">Vrsta</td>
<td width="20%">IP</td>
<td width="20%">Dodano (DD.MM.YY - HH.MM)</td>
<td width="20%">Status</td>
</tr>';
//start the loop to get all lines in the table..
for ($i=0; $i<=$lines; $i++) {
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
//close the table so HTML wont suffer :P
echo'</table>';
?>
This should produce this but ip column need to be link...
I think vprintf() is your friend.
<?php
$fmt = '<tr>
<td>%1$s</td>
<td>%2$s</td>
<td>%3$s</td>
<td>%4$s</td>
<td>%5%s</td>
</tr>';
for ($i=0; $i<=$lines; $i++)
{
// get each line and explode it..
$part = explode('|', $file[$i]);
// now start printing ..
vprintf($fmt, $part);
}
And put the width="20%" into your CSS.
I solve it alone with changing some values in input script "file writer"
$savestring = $title . "|" . $genre . "|<a href=http://www.example.com/ip?ip=" . $ip . ">" . $ip . "|" . $date . "|Za Naložit \n";
it works now ty anyway :)

Show selected text file in html page after submit button press

Good Day Everyone
Ok, so I have done as much as I understand and need some direction and help. Currently i'm very new to html/php so please bear with me. The plan is to list the text files from a Dir in a dropdown list, this I have done, now I would like to display the text file in the same page in a table upon submit button press. This is what I have so far, any input welcome as I am still learning!
The bash script is just a grep function to grab specific lines from the original file and copy it to /tmp.
Thanks Again
<html>
<head>
<title>Data Request</title>
</head>
<body>
<h1 align="center">Dispatch Report</h1>
<h2 align="center">Wrong Arrives Report</h2>
<table align="center" border="2">
<tr>
<td>Select Shift<br>
<form name="shiftfrm" id="shiftfrm">
<select name="shiftlist" id="shiftlist">
<option value="" selected="selected">--------</option>
<?php
$dir = opendir ("/var/www/files/");
while (false !== ($file = readdir($dir))) {
if (strpos($file, '.txt',1)) {
echo '<option value="' . $file . '">' . $file . '</option>';
}
}
?>
</select>
<input type="submit" id="submit" value="Submit"/>
<?php
if( ($handle = fopen( '/tmp/sh130418n.txt', 'r' )) !== false )
{
$output = '<table align="center" width="" border="2">';
while( ($data = fgetcsv( $handle )) !== false )
{
$output .= '<tr>';
foreach( $data as $value )
{
$output .= sprintf( '<td>%s</td>', $value );
}
fclose( $handle );
$output .= '</table>';
}
echo $output;
?>
</td></tr>
</table>
<?php
$output = exec('/var/www/cgi-bin/manualexceptget.sh');
echo "<pre>$output</pre>";
?>
</body>
</html>
assume <form method="post" action="">. Augment your file reading code:
if(!empty($_POST['shiftlist'])) {
$file = 'files/'.$_POST['shiftlist'];
if(file_exists($file)) {
if( ($handle = fopen( $file, 'r' )) !== false )
{
$output = '<table align="center" width="" border="2">';
while( ($data = fgetcsv( $handle )) !== false )
{
$output .= '<tr>';
foreach( $data as $value )
{
$output .= '<td>'.$value.'</td>';
}
$output .= '</table>';
}
echo $output;
fclose( $handle );
}
}
}
Edit: Fixed the isset() issue stated below. Changed some code, $handle was closed before reading was finished.
Edit2: I just put this in editor and saw many html tags, that were not placed properly (e.g. form not closed). Working sample at pastebin (tested on xampp)
Try Like this for (.txt) files..
<html>
<head>
<title>Data Request</title>
</head>
<body>
<h1 align="center">Dispatch Report</h1>
<h2 align="center">Wrong Arrives Report</h2>
<table align="center" border="2">
<tr>
<td>Select Shift<br />
<form name="shiftfrm" id="shiftfrm" method="POST">
<select name="shiftlist" id="shiftlist">
<option value="" selected="selected">--------</option>
<?php
$dir = opendir("files/");
while (false !== ($file = readdir($dir)))
{
if (strpos($file, '.txt', 1))
{
echo '<option value="' . $file . '">' . $file . '</option>';
}
}
?>
</select>
<input type="submit" name="submit" id="submit" value="Submit"/>
<br />
<?php
if (isset($_POST['submit']) && isset($_POST['shiftlist']))
{
if ($handle = file_get_contents('files/' . $_POST['shiftlist']))
{
$output = '<table align="center" width="" border="2">';
$output .= '<tr><td>';
echo $handle;
$output .= '</tr></td>';
$output .= '</table>';
} else
{
echo "No Content";
}
} else
{
echo "Please select the file";
}
?>
</td>
</tr>
</table>
</body>
</html>

Dynamic table (colons depend on the content of the table)

This php code shows me the content of a directory on my website:
<?php
$dir = opendir(getcwd());
?>
<body>
<table>
<tbody>
<tr>
<?php
while (($file = readdir($dir)) !== false) {
{
echo "<td>". $file ."</td>";
}
}
closedir($dir);
?>
</tr>
</tbody>
</table>
</body>
It puts the results in a table.
The problem is that the PHP code generates a <td> tag and store the results in it. So the final table has one <tr> and as many <td> tags as there are results.
What I want to have is a table with 3 columns (3 td) per each line (tr tag).
Is there a way to make the table dynamic and for each third <td> tag turns to be a <tr> tag
so the results look like this: (click here)
Instead of looking like this: (click here)
try this:
<?php
$dir = opendir(getcwd());
?>
<body>
<table>
<tbody>
<?php
$n = 0;
while (($file = readdir($dir)) !== false) {
{
if($n%3 == 0){echo "<tr>";}
echo "<td>". $file ."</td>";
$n++;
}
}
closedir($dir);
?>
</tbody>
you can use modulus to keep track of where you are in the loop.
Then, when you've hit a multiplication of 3, you restart the table row:
<?php
$dir = opendir(getcwd());
?>
<body>
<table>
<tbody>
<tr>
<?php
$counter = 0;
while (($file = readdir($dir)) !== false) {
{
if($counter % 3 == 0 && $counter != 0) echo "</tr><tr>";
echo "<td>". $file ."</td>";
$counter++;
}
}
closedir($dir);
?>
</tr>
</tbody>
</table>
</body>

Categories