Author - StudySection Post Views - 233 views
moodle-totara

How to create and download a CSV file in Moodle/Totara

  1. First, include the “csvlib.class.php” file:
    require_once ($CFG->libdir . '/csvlib.class.php');
  2. Now, call the constructor of the csv_export_writer class:
    $exportcsv = new csv_export_writer ( 'semicolon' );
    There are three arguments you can pass to the constructor:

    • The first is used to pass the delimiter that is used to separate fields. The strings “comma,” “tab,” “semicolon,” “colo,” and “cfg” can be used as delimiters.
    • The character that is used to determine the enclosures is supplied as the second argument.
    • In the third parameter, you can set the mime type of the file. Default mime type is “application/download”.
  3. Next, set the name of the file being exported:
    $exportcsv->set_filename ( “ExportUsersData” );
  4. To add the data in the csv file, you will have to call the add_data() function.This function takes only an array as an argument. Let’s say I’m making a csv file to house the user’s information. Here I am creating the header row in the file:
    $headers = array (
    ‘Name,
    'Email',
    'Address’'
    );
    $exportcsv->add_data ( $headers ); //Add Header Row
  5. Using the same function, you can create multiple rows in a csv file. Now, I’m adding the actual user information to the file:
    $userdata = array (
    array (
    "Test User1",
    "testuser1@gmail.com",
    "CHD"
    ),
    array (
    "Test User2",
    "testuser2@gmail.com",
    "AMT"
    )
    );
    foreach ( $userdata as $userrow ) {
    $exportcsv->add_data ( $userrow ); //Create a row for each user
    }
  6. If you want to display the csv data line by line, you can run the print_csv_data() method.
  7. Finally, you must execute the “download_file()” function to download the csv file. Here, I’m using the below code to download the csv file:
    $exportcsv->download_file();

This is the full example:
<?php
require_once '../../config.php'; // to include $CFG
require_once ($CFG->libdir . '/csvlib.class.php');
$exportcsv = new csv_export_writer ( 'semicolon' );
$exportcsv->set_filename ( "ExportUsersData" );
//CSV File columns
$headers = array (
'Name',
'Email',
'Address’'
);
$exportcsv->add_data( $headers ); //Add Header Row
//User Information Array
$userdata = array (
array (
"Test User1",
"testuser1@gmail.com",
"CHD"
),
array (
"Test User2",
"testuser2@gmail.com",
"AMT"
)
);
foreach ( $userdata as $userrow ) {
$exportcsv->add_data ( $userrow ); //Create row for each user
}
//Download the csv file
$exportcsv->download_file ();
?>

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri