PHP code example of sujan / php-csv-exporter

1. Go to this page and download the library: Download sujan/php-csv-exporter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sujan / php-csv-exporter example snippets

$xslt
composer 
$xslt
$columns = [ 'id', 'name', 'email' ];

$queryBuilder = User::limit(10); // Query Builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')
         ->export();
$xslt
$columns = [
    'id',
    'title',
    'user' => [ // user is a relation
        'name'
    ]
];

$queryBuilder = Post::with('user'); // Query builder

$exporter = new Exporter();
$exporter->build($queryBuilder, $columns, 'users.csv')
         ->export();
$xslt
    $servername = "localhost";
    $username = "root";
    $password = "password";
    $dbname = "laravel";

    $columns = [
        'id',
        'name',
        'email'
    ];

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT id, name, email FROM users");
        $stmt->execute();

        // set the resulting array to associative
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        $exporter = new Exporter();
        $exporter->build($stmt, $columns, 'users.csv)
                 ->export();
    }
    catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
    $conn = null;