Hi everone.

I've got a hard nut to crack here, at least for me.

I have a script that creates a tab delimited txt file using some constant values and entries from db table.

what I got look like this:

$result = $connector->query("SELECT * FROM new_part WHERE status=4");

$tab = "\t";
$cr = "\n";

//create line1 and line2 with constant values
$baseline1 = "MATERIAL NUMBER" . $tab . "MATERIAL TYPE" . $tab . "DESCRIPTION" . $cr;
$baseline2 = "RMMG1-MATNR" . $tab . "RMMG1-MTART" . $tab . "SKTEXT-MAKTX1" . $cr;

//assign them both to a different variable
$baseContent = $baseline1;
$baseContent .= $baseline2;

//create line3 with values from db table
while ($row = $connector->fetchArray($result)){
$tf1 = $row['tf1'];
$tf2 = $row['tf2'];
$tf3 = $row['tf3'];
// assign to variable
$baseline3 = $tf1 . $tab . $tf2 . $tab . $tf3 . $cr;
// add to $baseContent variable
$baseContent .= $baseline3;
}

$baseFile = "base.txt";
$dir = "temp/";
$baseFileHandle = fopen($dir . $baseFile, 'w') or die("Unable to create export file " . $baseFile . "!");
fwrite($baseFileHandle, $baseContent);
fclose($baseFileHandle);

the problem occures when there is more than 1 record with status=4. In such situation variable $baseline3 will be overwritten each passing of function.
I was thinking to on each pass of while ($row = $connector->fetchArray($result)) increment the number at the end, like $baseline3->$baseline4->$baseline5, etc. and add them to $baseContent. But I don't know how to do it.

Does anyone knows a way to get it working, or got other idea how to go around this problem.

Any help will be greatelly appreciated.

    fputcsv is your friend:

    <?php
    
    $baseFile = "base.txt";
    $dir = "temp/";
    $fhand = fopen($dir . $baseFile, 'w') or die("Unable to create export file " . $baseFile . "!");
    
    fputcsv($fhand, array("MATERIAL NUMBER", "MATERIAL TYPE", "DESCRIPTION"));
    fputcsv($fhand, array("RMMG1-MATNR", "RMMG1-MTART", "SKTEXT-MAKTX1"));
    
    $result = $connector->query("SELECT tf1, tf2, tf3 FROM new_part WHERE status=4");
    // Add all results to file
    while ($row = $connector->fetchArray($result)){
        fputcsv($fhand, $row);
    }
    
    fclose($fhand); 
    
      Write a Reply...