PHP code example of onlyphp / codeigniter3-csvimporter

1. Go to this page and download the library: Download onlyphp/codeigniter3-csvimporter 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/ */

    

onlyphp / codeigniter3-csvimporter example snippets


// Initialize the processor
$processor = new \OnlyPHP\CSVSimpleImporter\CSVImportProcessor();

// Set callback function for processing each row
$processor->setCallback(function($row, $rowIndex, $models) {
    try {
        // Process your row data here
        return [
            'code' => 200,
            'action' => 'create',
            'message' => 'Success'
        ];
    } catch (\Exception $e) {
        return [
            'code' => 500,
            'error' => $e->getMessage()
        ];
    }
});

// Start processing
$jobId = $processor->process('/path/to/your/file.csv');

$processor = new \OnlyPHP\CSVSimpleImporter\CSVImportProcessor();

// Set user ID for file ownership
$processor->setFileBelongsTo(1);

// Set HTML element ID for frontend progress tracking
$processor->setDisplayHTMLId('progress-bar-1');

// Configure CSV processing parameters
$processor->setMemoryLimit('1G')
         ->setDelimiter(',')
         ->setEnclosure('"')
         ->setEscape('\\')
         ->setChunkSize(1000)
         ->setRecordUpdateInterval(250)
         ->setSkipHeader(true);

// Load specific models for processing
$processor->setCallbackModel(['User_model', 'Product_model']);

// Set callback with loaded models
$processor->setCallback(function($row, $rowIndex, $models) {
    $userModel = $models['User_model'];
    $productModel = $models['Product_model'];

    try {
        // Your processing logic here
        $result = $userModel->createFromCSV($row);

        return [
            'code' => 200,
            'action' => 'create',
            'message' => 'User created successfully'
        ];
    } catch (\Exception $e) {
        return [
            'code' => 500,
            'error' => 'Row ' . $rowIndex . ': ' . $e->getMessage()
        ];
    }
});

// Start processing
$jobId = $processor->process('/path/to/your/encryptFileName.csv', 'originalFileName.csv');

// Kill a running process
$processor->killProcess($jobId);

// Check process status
$status = $processor->getStatus($jobId);

// Check status for all processes owned by a user
$status = $processor->getStatusByOwner($userId);

[
    'job_id' => 'csv_123456789',
    'total_process' => 100,
    'total_success' => 95,
    'total_failed' => 5,
    'total_inserted' => 80,
    'total_updated' => 15,
    'total_skip_empty_row' => 3,
    'display_id' => 'progress-bar-1',
    'estimate_time' => [
        'hours' => 0,
        'minutes' => 5,
        'seconds' => 30
    ],
    'file_name' => 'users.csv',
    'status' => 2, // 1=Pending, 2=Processing, 3=Completed, 4=Failed
    'error_message' => '[]',
    'percentage_completion' => '10',
    'last_check' => '2024-01-05 12:34:56'
]

// Success response
return [
    'code' => 200,
    'action' => 'create', // or 'update'
    'message' => 'Success message'
];

// Error response
return [
    'code' => 500,
    'error' => 'Error message'
];