How to add static value to csv in codeigniter?

Member

by lizzie , in category: PHP Frameworks , 5 months ago

How to add static value to csv in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 5 months ago

@lizzie 

To add a static value to a CSV file using CodeIgniter, you can follow these steps:

  1. Load the CSV library in your controller: $this->load->library('csv');
  2. Create a static value or a set of static values: $staticValue = 'Sample Value'; // or $staticValues = array('Value 1', 'Value 2', 'Value 3');
  3. Load the CSV file and append the static value(s) to each row: // Load the CSV file $filePath = './path/to/your.csv'; $csvData = $this->csv->from_path($filePath)->get(); // Iterate through each row and append the static value(s) foreach ($csvData as &$row) { // For a single static value $row[] = $staticValue; // For multiple static values foreach ($staticValues as $value) { $row[] = $value; } } // Generate the updated CSV data $updatedCsvData = $this->csv->from_array($csvData)->get(); // Save the updated CSV data $outputFilePath = './path/to/updated.csv'; $this->csv->to_path($outputFilePath, $updatedCsvData);
  4. The updated CSV file will be saved with the added static value(s) at the specified outputFilePath.


Make sure to replace ./path/to/your.csv with the actual path to your CSV file and ./path/to/updated.csv with the desired output file path.