I need to get those who has more than 15 points in particular time and show in HTML page (table), the data is loaded from a CSV. The CSV file looks like below:
test.csv file
Name,Sun 0:00,Sun 1:00,Sun 2:00,Sun 3:00,Sun 4:00
John,2,0,0,0,16
Alex,0,0,0,0,0
Dan,0,15,0,0,0
Jeff,0,0,30,0,0
Peter,12,0,0,0,0
Joe,0,0,0,0,340
Bill,0,0,340,0,0`
Here is the PHP file which process the CSV and shows in HTML table.
<html>
<body>
<?php
echo "<html><body><table>\n\n";
echo "<style>\ntable,th,td\n{\nborder:1px solid black;\nborder-collapse:collapse\n}\n</style> ";
$f = fopen("test.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
if ($cell >=15)
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>
</body>
</html>`
But this code only provides the output as like below without the column, row label. The second table is what I'm expecting in the output.
PS: If this is can be done in jQuery/Javascript, those ideas also welcome.
Ignoring the wrong markup (you duplicate some tags), your error is that you print a cell only if the value is greater than 15. You must print always the cell instead, and print content only if it is gt 15. Example:
<html>
<body>
<style>
table,th,td
{
border: 1px solid black;
border-collapse:collapse;
}
</style>
<table>
<?php
$f = fopen("test.csv", "r");
$i = 0;
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
$j = 0;
foreach ($line as $cell) {
echo "<td>";
if ($i==0 || $j==0 || $cell >= 15) {
echo htmlspecialchars($cell);
}
echo "</td>";
$j++;
}
echo "</tr>";
$i++;
}
fclose($f);
?>
</table>
</body>
</html>
if ($cell >=15) will fail for anything that is not an integer. Hence, all string values are ignored
Instead of that use
if (trim($cell) != '')
Related
Hellos,
I would like to do a simple telephone list. The csv data I do get from a csv list from an active directory powershell export.
the csv is comma seperated and it's looking that way:
#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser
name,"officephone"
firstnameA, lastnameA,"+49 12345 555 123"
firstnameB, lastnameB,"+49 12345 555 124"
firstnameC, lastnameC,"+49 12345 555 125"
firstnameD, lastnameD,"+49 12345 555 126"
So, and I would like to display it like that way (firstnameA, lastnameA in one row and the three digit number in the second row:
firstnameA, lastnameA 123
firstnameB, lastnameB 124
firstnameC, lastnameC 125
firstnameD, lastnameD 126
till yet I have that code:
<?php
echo "<html><body><table>\n\n";
$phonebook = fopen("database/itlocal.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr class='departmenttext'>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>
How can I skip the first two lines and also cut the telephone number I'd like to have it?
thanks in advance
Try this:
$lineCount = 0;
while (($line = fgetcsv($f)) !== false) {
if ($lineCount > 1) {
echo "<tr class='departmenttext'>";
foreach ($line as $key => $cell) {
if ($key == 1) {
$cell = substr($cell, -3);
echo '';
}
echo "<td>" . htmlspecialchars($cell) . "</td>";
if ($key == 1) {
echo "</tr>\n";
}
}
echo "</tr>\n";
}
$lineCount = $lineCount + 1;
}
I'm having some trouble with my website. It uses HTML with embedded PHP. I have a schedule page that pulls information from a CSV file and displays it as a table on the site, to make it easier to update schedules, but it's not working for some reason. Here's the code (I cut out the unimportant stuff).
<html>
<head>
</head>
<body>
<table>
<?php
$f = fopen("schedule.csv", "r");
while (($line = fgetcsv($f)) !== false)
{
$row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array)
$cells = explode(";",$row);
echo "<tr>";
foreach ($cells as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
?>
</table>
</body>
</html>
However, when I try opening the webpage, all I get is:
foreach ($cells as $cell) { echo ""; } echo "\n"; } fclose($f); ?>
" . htmlspecialchars($cell) . "
I can't tell if I'm missing a punctuation or what. I basically copied it from another site that another programmer recommended to someone else, so I don't know what's wrong.
Try using this code:
$f = fopen("schedule.csv", "r");
while (($line = fgetcsv($f)) !== false)
{
echo "<tr>";
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>";
}
fclose($f);
Use single quotes instead
<html>
<head>
</head>
<body>
<table>
<?php
$f = fopen('schedule.csv', 'r');
while (($line = fgetcsv($f)) !== false)
{
$row = $line[0]; // We need to get the actual row (it is the first element in a 1-element array)
$cells = explode(';',$row);
echo '<tr>';
foreach ($cells as $cell)
{
echo '<td>' . htmlspecialchars($cell) . '</td>';
}
echo '</tr><br/>';
}
fclose($f);
?>
</table>
</body>
</html>
This question already exists:
automatically build a table in html based on a csv file [duplicate]
Closed 7 years ago.
I found this solution here
but I cannot get it to work, but it is probably something basic, Can anyone advise?
this is what the php looks like on my server:
$ cat table.php
<?php
echo "<html><body><table>\n\n";
= fopen("temp.csv", "r");
while (( = fgetcsv()) !== false) {
echo "<tr>";
foreach ( as ) {
echo "<td>" . htmlspecialchars() . "</td>";
}
echo "</tr>\n";
}
fclose();
echo "\n</table></body></html>";
this is what the csv file looks like:
$ cat temp.csv
DeviceName,counter1,counter2,counter3,counter4
DeviceName1,85%,87%,75%,63%
DeviceName2,85%,85%,74%,70%
DeviceName3,80%,81%,70%,66%
DeviceName4,78%,82%,70%,74%
DeviceName5,77%,75%,68%,58%
DeviceName6,77%,72%,66%,58%
then I go to the address in the browser
webaddressgoeshere/php/table.php
I get the following on my console:
GET webaddressgoeshere/php/table.php 500 (Internal Server Error)
Navigated to webaddressgoeshere/php/table.php
my answer below, i was missing a variable when I copeid and pasted. oops!!
$ cat table.php
<?php
echo "<html><body><table>\n\n";
$f = fopen("temp.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
<?php
if($csv_fp = fopen('csv_file.csv','r'))
{
echo '<table>';
while ($data = fGetCsv ($csv_fp, 8096, ","))
{
// you can view the array $data holds by print_r($data); exit();
echo '<tr>';
foreach($data as $d)
{
echo '<td>'.$d.'</td>';
}
echo '</tr>';
}
echo '</table>';
}
else
{
echo 'Failed to open file';
}
Looks like your code have some syntax issues. The code is incomplete. Here is the code:
<?php
echo "<html><body><table>\n\n";
$xfile = fopen("temp.csv", "r"); // variable was missing
while (( $line = fgetcsv($xfile)) !== false) { // here too
echo "<tr>";
foreach ( $line as $ value) { // and here
echo "<td>" . htmlspecialchars($value) . "</td>"; // and here
}
echo "</tr>\n";
}
fclose($xfile); // and here
echo "\n</table></body></html>";
?>
Thanks to some help I got here, I managed to pull specific rows and columns of data from a CSV into an HTML table using PHP.
The working table is here: http://digitaldemo.net/table/table.php
Here is the data in the CSV (screenshot)
https://www.dropbox.com/s/5pa0ruxxnydwhr4/Screenshot%202014-11-17%2012.19.26.png?dl=0
and my code:
<?php
echo "<table cellspacing=\"5\" cellpadding=\"5\">";
echo "<thead><tr align=\"center\">";
echo "<td>Cumulative<br />Total Returns (%) as of [10]</td>";
echo "<td>Quarter</td>";
echo "<td>YTD</td>";
echo "<td>1YR</td>";
echo "<td>3YR</td>";
echo "<td>5YR</td>";
echo "<td>10YR</td>";
echo "<td>Since<br>Inception<sup>1</sup></td>";
echo "</tr></thead>";
$filter = array(
'2' => '1,2,3,4,5,6,7,8',
'3' => '1,2,3,4,5,6,7,8',
'5' => '1,2,3,4,5,6,7,9'
);
if (($handle = fopen("source.csv", "r")) !== FALSE) {
$row = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(array_key_exists($row, $filter))
{
$columns = explode(",", $filter[$row]);
echo "<tr align=\"center\">";
for($i = 0; $i <= count($data); $i++)
{
if(in_array($i, $columns))
{
echo "<td>" . $data[$i - 1] . "</td>";
}
}
echo "</tr>";
}
$row++;
}
}
echo "</table>";
?>
My question now is how do I find / replace text in the CSV so that it shows up the way that I want in the table (e.g. changing Row_1 in the table above to Apple)?
Also, how can I auto format any numbers (including those w/ leading zeros) to two decimal places?
Thanks in advance!
Cynthia
I'm very new to PHP and have been experimenting with combining with css to turn a CSV file into a table on my site. I wondered if you could select to show just a range of rows from a CSV file, e.g. rows 5 to 20, and found the code below to show certain columns.
Is there a simple way to switch this to show selected rows instead?
<?php
$row = 1;
if (($handle = fopen("donors.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<td style="border-top: 1px solid rgb(111,180,224); border-left: 1px solid rgb(111,180,224); border-bottom: 1px solid rgb(111,180,224);" align="left" bgcolor="#0066cc" height="36" valign="middle" ><b><font color="#ffffff" size="2"> '.$value.' </font></b></td>';
}else{
echo '<td style=" border-bottom: 1px solid rgb(111,180,224);" sdval="9" sdnum="1040;" align="left" bgcolor="#ffffff" height="25" valign="middle"><font color="#000000" size="2"> '.$value.' </font></td>';
}
}
if ($row == 1) {
echo '</tr>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
echo '</center>';
fclose($handle);
}
?>
I'm not really sure to undestand what you want to display.
But on the test website i added a table which diplay a range of lines.
Here is the code of this table:
echo "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>
<link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">";
$file = '/var/www/test/test.csv';
echo "<h1><b>Lines X to Y</b></h1>";
echo "<table>"; //create the html table
$x=2; //define the first line to display
$y=4; //define the last line to display
$line_counter=0;
foreach ($lines as $line) { //loop to fill your table with csv data
if($x<=$line_counter && $line_counter<=$y)
{
$expl = explode(",", $line); //explode each line on "," to create the rows
echo "<tr>"; //create each line of your table
$c=0;
while ($c<=26) { //loop to extract rows 5 to 20
if(($c % 2) == 1){ //odd rows
echo "<td class=\"odd\">".$expl[$c]."</td>";
}
elseif(($c == 0)){
echo "<td class=\"even\">".$expl[$c]."</td>";
}
else{
echo "<td class=\"even\">".$expl[$c]."</td>"; //even rows
}
$c++;
}
echo "</tr>";
$x++;
}
$line_counter++;
}
echo "</table>";
echo "<h1><b>content of the file</b></h1>";
foreach ($lines as $line) { //loop to fill your table with csv data
echo "$line<br/>" ;
}
The CSS is always in a separate file, and is the same as my first answer.
Select a range of rows:
i adapted this code from an old project, but it should work.
<?php
echo "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>
<link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">";
$file = '/volume1/web/Print data.csv';
$lines = file($file);
if (file_exists($file)){
//echo "$file exists<br/>";
echo "<table>"; //create the html table
foreach ($lines as $line) { //loop to fill your table with csv data
$expl = explode(",", $line); //explode each line on "," to create the rows
echo "<tr>"; //create each line of your table
$c=5;
while ($c<=20) { //loop to extract rows 5 to 20
if(($c % 2) == 1){ //odd rows
echo "<td class=\"odd\">".$expl[$c]."</td>";
}
else{
echo "<td class=\"even\">".$expl[$c]."</td>"; //even rows
}
$c++;
}
echo "</tr>";
}
echo "</table>";
}
else{
echo "file is not here: $file";
}
?>
For the css:
you should use an external .cssfile with this code
.odd {
border-top: 1px solid rgb(111, 180, 224);
border-left: 1px solid rgb(111, 180, 224);
border-bottom: 1px solid rgb(111, 180, 224);
background:#0066cc;
color:white;
font-weight:bold;
font-size:12px;
}
.even {
border-bottom: 1px solid rgb(111, 180, 224);
background: #ffffff;
color:#000000;
font-size:12px;
}
To check this css you can see this jsfiddle
I hope it will help you.
Edit
Fixed some missing ;. Added an picture of the result
1) Fetch your CSV file to an array with the below function (source: jaywilliams # GIST)
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
2) Iterate over the desired range of rows
$data = csv_to_array($file,$delimiter);
echo "your table header stuff"; //<table> ...
//first row numbered as 1
$from_row = 2; //second
$to_row = 50;
//first row numbered as 0
for($i = from_row-1; $i< $to_row-1; $i++)
{
echo "<tr>"; //start row
//now, every row of the CSV file is an array and consists of fileds=>values
//so now you are dealing with columns
foreach($data[$i] as $key=>$value
{
echo "<td>" . $value . "</td>";
}
echo "</tr>"; //end row
}
echo "your table footer stuff"; //</table> ...
From here, I guess you can do whatever your want, using CSS to style the tables. What is important, if you want the table header, you can feed the function with a CSV file with col names saved in first row.