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

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

Related

php import csv first row table header

I have a csv file that I will import on a php page. But he must set the first row as a table header (th).
I use this script but for now I have only table rows (tr)
<?php
echo "
<table class='table table-striped table-hover'>\n\n";
$f = fopen("myfile.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>
";?>
Can someone help me with this code to set the first row as header and the rest as tr.
Thanks
$flag = true;
while(..) {
if($flag) {
// Proceed header here
$flag = false;
continue;
}
// the rest rows
}
Updated
echo "<table class='table table-striped table-hover'>\n\n";
$f = fopen("myfile.csv", "r");
$flag = true;
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
if($flag) {
// Proceed header here
foreach ($line as $cell) {
echo "<th>" . htmlspecialchars($cell) . "</th>";
}
echo "</tr>\n";
$flag = false;
continue;
}
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n </table>";

Break PHP while loop if variable is empty

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.

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

Reverse Sort by particular column for each loop in php

This is csv file :
40405,Vodafone,2
405806,Aircel,1
41303,Etisalat,1
45201,MobilFone,3
51010,Telkomsel,1
63903,Zain,1
63905,yu,2
64005,Airtel,1
I tried using the rsort, ksort, asort, but unable to sort according to the column.
Echoing using the for each loop in php : I am trying to sort the whole data according to the 3rd column in reverse order( descending) ,
$f = fopen("file.csv", "r");
while (($line = fgetcsv($f)) !== false)
{
echo "<tr id='trdata'>";
foreach ($line as $cell)
{
echo "<td>" . htmlspecialchars($cell). "</td>";
}
echo "</tr>\n";
}
Thanks.
$f = fopen("file.csv", "r");
$fileData = array();
while (($line = fgetcsv($f)) !== false) {
$fileData[] = $line;
}
echo arrayAsTable($fileData) . "<br />";
usort($fileData, function($a, $b) {
return $b[2] - $a[2];
});
echo arrayAsTable($fileData);
function arrayAsTable($array)
{
$out = "<table>";
foreach ($array as $line) {
$out .= "<tr id='trdata'>";
foreach ($line as $cell) {
$out .= "<td>" . htmlspecialchars($cell) . "</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
return $out;
}

Categories