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>
Related
I have a CSV (attached below) that I am looping through with PHP. Note: the Peter entry is missing an Extension number.
How do I break the loop if any $cell (like Extension) is empty
<?php
echo "<html><body><table>\n\n";
$f = fopen("users.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>";
~
Forename,Surname,Extension
Jim,Carey,1973
Peter,Robertson,
Try using this -
<?php
echo "<html><body><table>\n\n";
$f = fopen("users.csv", "r");
while (($line = fgetcsv($f)) !== false) {
$row = "<tr>";
$is_empty = false;
foreach ($line as $cell) {
if ($cell !== '') {
$row .= "<td>" . htmlspecialchars($cell) . "</td>";
} else {
$is_empty = true;
}
}
$row .= "</tr>\n";
if ($is_empty) {
continue;
} else {
echo $row;
}
}
fclose($f);
echo "\n</table></body></html>";
?>
This solution will print the row only if all the fields have value. You can use break instead of continue, if you want to break the loop.
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>";
?>
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) != '')
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";
}
}
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?