How to add static value to csv in codeigniter?

Member

by lizzie , in category: PHP Frameworks , a year ago

How to add static value to csv in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , a year 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.

Related Threads:

How to add a number to value if already exist in codeigniter
How to export csv file in codeigniter
How to get value from another model in codeigniter?
How to add headers while converting json to csv in php?
How to add headers while converting json to csv in php?
How to add a line break in woocommerce product titles on static pages?