Php-mysql Course

- parseCSV is an easy to use PHP class to read and write CSV data properly.
- Download parseCSV 0.4.3 beta.

Features

• Supports enclosed values, enclosed commas, double quotes and new lines.
• Automatic delimiter character detection.
• Sort data by specific fields/columns.
• Easy data manipulation.
• Basic SQL-like conditions, offset and limit options for filtering data.
• Store CSV data into an array, convert array to CSV data.
• Error detection for incorrectly formatted input. It attempts to be intelligent, but can not be trusted 100% due to the structure of CSV, and how different programs like Excel for example outputs CSV data.
• Support for character encoding conversion using PHP's iconv function (requires PHP 5).

Examples
- These examples use this CSV, saved in "books.csv":
rating,title,author,type,asin,tags,review
0,The Third Secret,Steve Berry,Book,0340899263,,need to find time to read this book
3,The Last Templar,Raymond Khoury,Book,0752880705,,
4,The Da Vinci Code (Hardcover),Dan Brown,"     Book     ",0385504209,book movie danbrown bestseller davinci,

1. Read a CSV file (with auto-detect delimiter character) and store data into an array:
<?php
// include parseCSV class
include('parsecsv/parsecsv.class.php');

// create new parseCSV object
$csv = new parseCSV();

// Parse 'books.csv' using automatic delimiter detection
$csv->auto('books.csv');

// if you know the delimiter, set the delimiter character (if not the default comma)
// $csv->delimiter = "\t";         // tab delimited

$csvdata = $csv->data;               // gets csv data into an array

// output result
echo '<pre>';
print_r($csvdata);
echo '</pre>';
?>

Result:
Array
(
  [0] => Array
    (
      [rating] => 0
      [title] => The Third Secret
      [author] => Steve Berry
      [type] => Book
      [asin] => 0340899263
      [tags] => 
      [review] => need to find time to read this book
    )
  [1] => Array
    (
      [rating] => 3
      [title] => The Last Templar
      [author] => Raymond Khoury
      [type] => Book
      [asin] => 0752880705
      [tags] => 
      [review] => 
    )
  [2] => Array
    (
      [rating] => 4
      [title] => The Da Vinci Code (Hardcover)
      [author] => Dan Brown
      [type] =>    Book   
      [asin] => 0385504209
      [tags] => book movie danbrown bestseller davinci
      [review] => 
    )
)

2. Read the CSV file, sorting data by "title".
<?php
// include parseCSV class.
include('parsecsv/parsecsv.class.php');

// create new parseCSV object
$csv = new parseCSV();

$csv->sort_by = 'title';

// Parse 'books.csv' using automatic delimiter detection...
$csv->auto('books.csv');

$csvdata = $csv->data;               // get csv data into an array

// output result
echo '<pre>';
print_r($csvdata);
echo '</pre>';
?>

Result:
Array
(
  [The Da Vinci Code (Hardcover)] => Array
    (
      [rating] => 4
      [title] => The Da Vinci Code (Hardcover)
      [author] => Dan Brown
      [type] =>    Book   
      [asin] => 0385504209
      [tags] => book movie danbrown bestseller davinci
      [review] => 
    )
  [The Last Templar] => Array
    (
      [rating] => 3
      [title] => The Last Templar
      [author] => Raymond Khoury
      [type] => Book
      [asin] => 0752880705
      [tags] => 
      [review] => 
    )
  [The Third Secret] => Array
    (
      [rating] => 0
      [title] => The Third Secret
      [author] => Steve Berry
      [type] => Book
      [asin] => 0340899263
      [tags] => 
      [review] => need to find time to read this book
    )
)

3. Convert 2D array to CSV, and send headers to browser to treat output as a file and download it.
<?php
// include parseCSV class.
include('parsecsv/parsecsv.class.php');

$csvHeader = array('category', 'link');              // csv header

// 2 dimensional array with data for csv (according to header)
$csvData = array(
  array('tutorials', 'https://coursesweb.net/php-mysql/tutorials_t'),
  array('scripts', 'https://coursesweb.net/php-mysql/scripts_s2'),
  array('resources', 'https://coursesweb.net/php-mysql/download_l2')
 );

// create new parseCSV object
$csv = new parseCSV();

// convert 2D array to csv data and output as a file to download it
// (filename, csv-data, csv-header, delimiter)
$csv->output('coursesweb.csv', $csvData, $csvHeader, ',');
?>

Result this CSV:
category,link
tutorials,https://coursesweb.net/php-mysql/tutorials_t
scripts,https://coursesweb.net/php-mysql/scripts_s2
resources,https://coursesweb.net/php-mysql/download_l2

- In the archive with parseCSV class you'll find other examples too.
parseCSV Web Site.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
<nav><ul>
 <li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
 <li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
}
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
parseCSV

Last accessed pages

  1. Node.js HTTP Module (435)
  2. Bind Tool and Control Points (4355)
  3. Node.js Move and Copy Directory (19967)
  4. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (55410)
  5. PHP Script Website Mini-Traffic (7073)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (202)
  2. Read Excel file data in PHP - PhpExcelReader (72)
  3. The Four Agreements (65)
  4. PHP Unzipper - Extract Zip, Rar Archives (64)
  5. Get and Modify content of an Iframe (50)
Chat
Chat or leave a message for the other users
Full screenInchide