PHP and CSV, Oh My!
How to read in CSV files using PHP
By Scott Kondor
Overview
For this tutorial, viewers will learn how to use PHP to read in and output CSV file
contents. The presentation will use the dynamic scripting language combined with
xHTML and CSS. It is assumed that users have used CSS, HTML and PHP prior to
starting this tutorial and also that users have a web server to host their own
websites.
Intended Audience
Comma-Separated Value files are often used in the scientific community. It is not
a well known format, but is relatively easy to work with. Also, beginning PHP
students could find the tutorial useful and will gain a better understanding of the
various functions in the language.
Video Events
0:00 – Introduction and 15-Second Pitch
0:24 – A few words
0:45 – An example of a CSV file
0:58 – How to create a CSV file with Microsoft Excel 2007
1:27 – Reading in a CSV file using PHP functions file and explode
1:58 – Outputting CSV file contents with CSS, PHP and XHTML
2:27 – Styling the output using CSS
2:50 – The actual code in .php, .css and .csv files
3:15 – Web output of table produced in tutorial
3:29 – Final code overview
3:44 – Start of conclusion with contact information
4:09 – Conclusion
4:18 – Final code comparison
5:06 – End of movie
Definitions
CSV – Comma-Separated Values
A file format used for storage of data structured in a table form. Each line in the
CSV file corresponds to a row in the table. Within a line, fields are separated by
commas, each field belonging to one table column. CSV files are often used for
moving tabular data between two different computer programs, for example
between a database program and a spreadsheet program.
PHP – PHP: Hypertext Processing
A scripting language originally designed for producing dynamic web pages. It has
evolved to include a command line interface capability and can be used in
standalone graphical applications. While PHP was created by Rasmus Lerdorf in
1995, the main implementation of PHP is now produced by the PHP Group as
there is no formal specification. Released under the PHP License, it is considered
to be free software.
CSS – Cascading Style Sheets
CSS can be used to define colors, fonts, layout, and other aspects of document
presentation. It is designed primarily to enable the separation of document
content (written in HTML or a similar markup language) from document
presentation (written in CSS)
XHTML – Extensible Hypertext Markup Language
It provides a means to describe the structure of text-based information in a
document - by denoting certain text as links, headings, paragraphs, lists, and so
on - and to supplement that text with interactive forms, embedded images, and
other objects. XHTML can also describe, to some degree, the appearance
and semantics of a document, and can include embedded scripting
language code.
Final Code
$var){
$array[$key] = explode(',', $var);
}
echo '';
foreach ($array as $key => $value) {
$i++;
echo '';
echo '' . $array[$key][0] . '';
echo '' . $array[$key][1] . '';
echo '';
}
echo '';
?>
CSS Formatting
table.samples {
border: 1px solid #CCCCCC;
margin-left: auto;
margin-right: auto;
}
tr.d0 td {
font: 14px Verdana;
background-color: #FFFFFF;
}
tr.d1 td {
font: 14px Verdana;
background-color: #EEEEEE;
}
References
PHP Official Website
www.php.net
W3Schools
www.w3schools.com
Contact
If you have any further questions or comments, please contact me using the form
provided on www.internapse.com.
Thank you for watching!