How to get td data from a table in PHP - php

I am currently stuck at this problem right now where I don't seem to have any idea how to implement this. I have this table which is populated from a csv file in php and I want to get the data logged in order to calculate the daylight hours.
How can I get the data from the td "dateLogged", I did that in jquery before using children but I do not seem to be able to do that in php
//print out uploaded file.csv
echo "<html><body><table BORDER=1>\n\n";
echo " <tr><th>AvgCrown</th><th>MinCrown</th><th>MaxCrown</th><th>dateLogged</th><th>DaylightHours</th></tr>";
$f = fopen("./uploads/" . $_FILES["uploadedfile"]["name"], "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "<td>" . calcDaylight() . "</td>";
echo "<tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
Here is a sample how the table looks like
Any help will be most welcome.

There's no need to parse the HTML if you can just grab the data while you're generating it.
Something like this should be all you need:
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
// Save the value before you output
$lastColValue = htmlspecialchars($cell);
echo "<td>" . $lastColValue . "</td>";
}
// If you want to store all log dates in an array:
$logDates[] = $lastColValue;
// Or if you want to do something with that value in calcDaylight:
echo "<td>" . calcDaylight( $lastColValue ) . "</td>";
echo "<tr>\n";
}
fclose($f);
Incidentally, if you do find you need to parse the HTML and are only familiar with jQuery, this library may strike your fancy:
http://code.google.com/p/phpquery/

this?
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>".htmlspecialchars($cell)."</td>";
}
echo "<td>" . calcDaylight( $cell[3] ) . "</td>";
echo "<tr>\n";
}
i might be wrong but 4th column is the field you want, no?

Related

How to parse a multi sheet csv into html table using php

I have a csv file which contains 2 sheets inside. I am trying to execute the csv file and convert it into a html table using php. but it is taking and displaying table of just one sheet. I am unable to find the other sheet's information.
For this I am using the following code,
<?php
echo "<table border='1' align='center' width='70%'>\n\n";
$f = fopen("../uploads/docs/test.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table><br>";
echo "<table border='1' align='center' width='70%'>\n\n";
$f = fopen("../uploads/docs/testing.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table>";
?>
Please help me by which i can parse csv having multiple sheets in it to a html table

Why is my table not showing?

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>

PHP CSV dynamic table want to make all entries in fifth column links

I have a dynamic table created from my PHP script. The information is sourced from a CSV file. I would like everything in the fifth column to be a link to another page. So that would be every array index/key 4 is a link. Unsure on how i would modify the code below to achieve this?
echo "<html><body><table>\n\n";
echo "<tr><td>First</td><td>Last</td><td>Email<td>Address</td
<td>File</td></tr>";
$f = fopen("filelog.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
Add a counter to your loop and look for every 5th iteration.
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
$i = 0;
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
$i++;
if($i % 5 === 0) {
echo "<td>" . yourlinkstuff . "</td>";
}
}
echo "</tr>\n";
}
I am using modulus because it's unclear from your question whether or not there are more than 4 "cells" per "line". If the foreach($line as $cell) always iterates 4 times, you don't need a counter. You could just tag on the 5th column after that loop, like so:
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "<td>" . yourlinkstuff . "</td>";
echo "</tr>\n";
}
You could add a counter and when you reach the 4th element, output a link.
$i = 1;
foreach ($line as $cell) {
if ($i == 4) {
// Create the link.
echo "<td> </td>";
}
else {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
$i++;
}
Thank you all for your input, I really appreciate it. I have a table that has five columns, "First, Last, Email, Address, File". Everything in the "File" column is a link to that file. So yes in the array starting at index 0 the "File" data will be at index 4. I didn't want an extra column. This is code that ended up doing the job, thank you!!
echo "<html><body><table>\n\n";
echo "<tr><td>First</td><td>Last</td><td>Email<td>Address</td>
<td>File</td></tr>";
$fp = fopen("filelog.csv", "r");
while (($line = fgetcsv($fp)) !== false) {
echo "<tr>";
$i=1;
foreach ($line as $cell) {
if(($i % 5) == 0) {
echo "<td><a href=" . htmlspecialchars($cell) . ">"
.htmlspecialchars($cell) . "</a></td>";
}
else {
echo "<td>" . htmlspecialchars($cell) . "</td>";
$i++;
}
}
echo "</tr>\n";
}
fclose($fp);
echo "\n</table></body></html>";

automatically build a table in html based on a csv file + can't get possible solution to work [duplicate]

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>";
?>

How to remove the first row when it is in while loop

This is my PHP script,
printing the file.
$f = fopen("file_name", "r");
echo "<tbody>";
while (($line = fgetcsv($f)) !== false)
{
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>";
fclose($f);
echo "\n</table>";
?>
This is the file_name
NODE,CGR,TERMID,VMGW,ET
GMSC01,932,1337,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
GMSC01,932,1338,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
GMSC01,932,1337,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
GMSC01,932,1338,,1&-2&-3&-4&-5&-6&-7&-8&-9&-10&-11&-12&-13&-14&-15
Now i want to print the file except first row, print the remaining data from 2nd row to till end.
please suggest how to display the file except first row.
Use a counter variable like $counter = 0; and increment it on every iteration, than use an if condition to skip the first iteration using if($counter != 1)
$counter = 0;
while (($line = fgetcsv($f)) !== false) {
$counter++;
if($counter != 1) {
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
}
Simply read the first line OUT of the while loop :
$f = fopen("file_name", "r");
$line = fgetcsv($f);
echo "<tbody>";
while (($line = fgetcsv($f)) !== false)
{
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
echo "</tbody>";
fclose($f);
$f = fopen("file_name", "r");
echo "<tbody>";
$flag=0;
while (($line = fgetcsv($f)) !== false)
{
if($flag==0)
{
//skip the first line
$flag=1;
}
else
{
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";`
}
echo "</tr>\n";
}
echo "</tbody>";
fclose($f);
echo "\n</table>";
?>
Here's a more efficient version that doesn't stretch the condition over the entirety of the loop contents and uses the continue statement to short-cut the first iteration.
$first = true;
while (($line = fgetcsv($f)) !== false) {
if ($first) {
$first = false;
continue;
}
echo "<tr>\n";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
}

Categories