This topic is locked
[SOLVED]

 Explaination question Custom Button

12/18/2020 4:51:13 AM
PHPRunner General questions
I
Ivan van Oosterhout author

Hi,

I do not know what happens, can somebody please explain.
I want to write an array to a text document.

Whatever i do, i receive only the last row of the array. "Bread"
My main perpose is to query with a custom button to the Order Table and take out all the rows related to the PreOrder Table.
$data = $button->getCurrentRecord();

$som1 = str_pad($data["id"],7,0,STR_PADLEFT);

$idvalue = "ORDER".$som1 ;
$myFile = ("Orders/Article
".$idvalue.".dds");
$foodArray = ["Eggs", "Bacon", "HashBrowns", "Beans", "Bread"];
foreach ($foodArray as $food) {

$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = $food ."\r\n";

fwrite($fh, $stringData);

}

fclose($fh);
$num = 0;
while ( $num < $y )
{
// print $num . " ";
exec ("Orders/".$myFile.".dss");

exec ("Orders/".$myFile1.".dss");

$num++;
}

M
MikeT 12/18/2020

You probably need to fopen() with the correct option (probably a for append), otherwise you overwrite each entry with the next and the last then being the only one in the file.

D
Daniedb 12/18/2020



foreach ($foodArray as $food) {

$fh = fopen($myFile, 'w') or die("can't open file");

$stringData = $food ."\r\n";

fwrite($fh, $stringData);

}

fclose($fh);


My logic tell me, you open a new file after the foreach (Everytime create a new file) which only write one record, and create a new file
try this
$fh = fopen($myFile, 'w') or die("can't open file");

foreach ($foodArray as $food) {

$stringData = $food ."\r\n";

fwrite($fh, $stringData);

}

fclose($fh);
Might be the problem, just a wild guess
Cheers

Danie

I
Ivan van Oosterhout author 12/18/2020

Thank you very much Danie,
You have made my christmas SO NICE :-)
much admiration for you i did not see it anymore.