PHP and CSV, Oh My - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

PHP and CSV, Oh My

Description:

A Comma-Separated Values (CSV) file stores information in a list with a comma between each item. ... { font: 14px Verdana; background-color: #EEEEEE; ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 9
Provided by: Sco761
Category:
Tags: csv | php | verdana

less

Transcript and Presenter's Notes

Title: PHP and CSV, Oh My


1
PHP and CSV, Oh My!
  • Scott Kondor

2
A Quick Intro
  • A Comma-Separated Values (CSV) file stores
    information in a list with a comma between each
    item.
  • Ex dog, cat, bird, bunny, monkey, etc
  • PHP is a scripting language, primarily used for
    dynamic web pages and applications.
  • variable world
  • echo ltpgtHello variablelt/pgt
  • PHP can be used to read in CSV files and display
    the data in a proper format with the use of xHTML
    and CSS.

3
An Example CSV File
  • A typical CSV file can be created in Microsoft
    Excel by selecting the CSV (Comma Delimited)
    option from the Save As menu.
  • Example
  • Type, Amount
  • Dogs,3
  • Cats,2
  • Turtles,5
  • Bunnies,6
  • Birds,11
  • Other,7

4
Reading in a CSV File
  • Read the entire file into an array using the file
    function.
  • array file('files/pets.csv')
  • 0 gt Type,Amount
  • 1 gt Dogs,3
  • 2 gt Cats,2
  • Use the explode function to separate each value
    into its own element.
  • foreach(array as key gt var)
  • arraykey explode(',', var)
  • 1 gt Array (
  • 0 gt Dogs
  • 1 gt 3
  • )

5
Outputting CSV File Contents
  • Use a foreach loop in order to output the
    contents of the embedded array. Also, use CSS to
    add style to your plain table.
  • echo 'lttable class"samples"gt'
  • i 0 // Alternate row coloring
  • foreach (array as key gt value)
  • i
  • echo 'lttr classd' . (i 1) . '
    align"center"gt'
  • echo 'lttdgt' . arraykey0 . 'lt/tdgt'
  • echo 'lttdgt' . arraykey1 . 'lt/tdgt'
  • echo 'lt/trgt'
  • echo 'lt/tablegt'

6
Styling the Output
  • 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

To learn more about CSS, visit W3Schools for
tutorials and examples.
7
Final Code
  • lt?php
  • array file('files/pets.csv')
  • foreach(array as key gt var)
  • arraykey explode(',', var)
  • echo 'lttable class"samples"gt'
  • foreach (array as key gt value)
  • i
  • echo 'lttr classd' . (i 1) . '
    align"center"gt'
  • echo 'lttdgt' . arraykey0 . 'lt/tdgt'
  • echo 'lttdgt' . arraykey1 . 'lt/tdgt'
  • echo 'lt/trgt'
  • echo 'lt/tablegt'
  • ?gt

8
Conclusion
  • There are other ways to read in CSV files,
    including a function called fgetcsv.
  • Questions, Comments, Concerns?
  • Online version coming soon Interactive Resume
Write a Comment
User Comments (0)
About PowerShow.com