?
Solved

Display CSV file data using PHP

Posted on 2009-12-24
1
Medium Priority
?
217 Views
Last Modified: 2013-12-13
Hi There,

I have written the code below to read am reading the contents of a CSV file. The content in the CSV file has three colums with data and multiple rows:
content1, content2, content3
content1, content2, content3
content1, content2, content3

I need to display this as a table in a webpage where content1, content2 and content3 would be in their own cell. When displayed in the webpage there should be three rows. Any suggestions on how i can get this done?

See code below. Speedy response is very much appreciated.

$dir_name = "C:/apache/htdocs/E-Books.txt";
$fh = fopen($dir_name, "r");
$row = 1;

while(($data = fgetcsv($fh, 1000, ",")) !== FALSE)
{
    $num = count($data); // count the # of lines that exist
    $row++;  //increment the row variable to the next line
   
//output current line to the screen.
    for ($i=0; $i < $num; $i++)
      {
          echo $data[$i]."\n";
      }
}

//close file handler and delete file
fclose($fh);
0
Comment
Question by:motolanix1
1 Comment
 
LVL 14

Accepted Solution

by:
jb1dev earned 1000 total points
ID: 26121125
Does this work.

$dir_name = "C:/apache/htdocs/E-Books.txt";
$fh = fopen($dir_name, "r");
$row = 1;

echo "<table>\n";
while(($data = fgetcsv($fh, 1000, ",")) !== FALSE)
{
    echo "<tr>\n";
    $num = count($data); // count the # of lines that exist
    $row++;  //increment the row variable to the next line
   
//output current line to the screen.
    for ($i=0; $i < $num; $i++)
      {
          echo "<td>\n";
          echo $data[$i]."\n";
          echo "</td>\n";
      }
    echo "</tr>\n";
}
echo "</table>\n";

//close file handler and delete file
fclose($fh);

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question