-3

I'm trying to use 'for loop' and write directly to csv without inserting into database and export as csv file.

<?php 
 require_once('function.php');

 for ($i=0; $i < 6000; $i++) { 
    # code...

   $colum1= generatePassword(5);
   $colum2 = serial_number(5);
   #write directly to csv file 

 }
Iamgisnet
  • 171
  • 3
  • 3
  • 12

2 Answers2

2

Check this.

<?php

$words = array (
    array('One', 'Two', 'Three', 'Four'),
    array('1', '2', '3')
);

$fp = fopen('output_file.csv', 'w');

foreach ($words as $word) {
    fputcsv($fp, $word);
}

fclose($fp);
?>
Marios Nikolaou
  • 1,326
  • 1
  • 13
  • 24
1

If you want to write to a csv file there is the fputcsv function that can be used. But is depends on what you are writing and how you are organising the data. Here is a sample of code.

<?php

 $list = array (
   array('name', 'age', 'mobile'),
   array('Doe', 24, 65676767),
   array('Thomas', 67, 98765637),
 );

 $fp = fopen('file.csv', 'w');

 foreach ($list as $fields) {
    fputcsv($fp, $fields);
 }

 fclose($fp);
?>
Ulrich Dohou
  • 1,509
  • 14
  • 25