Lynn
Lynn

Reputation: 83

PHP strings concatenation

My code cycles through a CSV file, converting it to XML:

<?php
for ($i = 1; $i < $arraySize; $i++) {
    $n = 0;
    if (substr($csv[$i][0], 0, 1) == $let) {
        $surName = $dom->createElement('name');
        $name = $csv[$i][0];
        $nameText = $dom->createTextNode($name);
        $surName->appendChild($nameText);

        $text = str_replace(chr(94), ",", $csv[$i][4]);
        $n = $i + 1;
        $next = $csv[$n][0];

        while ($next == 'NULL') {
            $repl = str_replace(chr(94), ",",  $csv[$n][4]);
            $text = $repl;

            $n++;
            $next = $csv[$n][0];
        }
        $bio = $dom->createElement('bio');
        $bioText = $dom->createTextNode($text);
        $bio->appendChild($bioText);
        $person = $dom->createElement('person');
        $person->appendChild($surName);
        $person->appendChild($bio);
        $people->appendChild($person);
    }
}
$xmlString = $dom->saveXML();
echo $xmlString; 
?>

The problem is the $text = $repl; Typing $text .= $repl; brings:

error on line 1 at column 1: Document is empty.

but omitting the . just gives the last line of text. the backup code works: public function test($let){ $csv = $this->readCSV("data\AlphaIndex1M.csv"); $arraySize=sizeof($csv); $let = strtoupper($let); //echo ''; for($i=1; $i echo $csv[$i][0];// .' echo ', -->'.$csv[$i][4]; $n = $i+1; $next = $csv[$n][0]; //if($next == 'NULL'){ } while($next == 'NULL'){ echo $csv[$n][4]. " "; $n++; $next=$csv[$n][0]; } //echo '' echo ''; } } //echo '' }

Upvotes: 0

Views: 85

Answers (1)

Rizier123
Rizier123

Reputation: 59681

You have to initialize your $text before you can append stuff!

So write this before you use it:

$test = "";

(before the while loop or even before the for loop if you want all to be appended)

Upvotes: 2

Related Questions