PHP function inserts values in wrong place
<p>The code below gets a list of all the .csv files in a directory. I have created the function <strong>csvcolumnsum</strong> to extract some information from each file (the sum of all values in column 3) and inject it within each list item.</p>

<p>The html output should look something like this:</p>

<pre><code>&lt;li&gt;Filename (sum)&lt;/li&gt;
</code></pre>

<p>But it comes out like this:</p>

<pre><code>sum&lt;li&gt;Filename ()&lt;/li&gt;
</code></pre>

<p>Can anyone point out why this is happening? Here is the relevant code.</p>

<pre><code>require("csvcolumnsum.php");

echo "&lt;ul class=\"users\"&gt;";
$handle=opendir("data");
while (($file = readdir($handle))!==false) {
    $file = preg_replace("/\\.[^.\\s]{3}$/","",$file);
    if (preg_match('/[^\.]/i', $file)) {
        $value = csvcolumnsum("data/".$file.".csv",3);
        echo "&lt;li&gt;&lt;a href=\"?form_name=$file\"&gt;$file(";
        echo $value;
        echo ")&lt;/a&gt;&lt;/li&gt;";
    }
}
closedir($handle);
echo "&lt;/ul&gt;";
</code></pre>

<p><strong>csvcolumnsum.php</strong></p>

<pre><code>function csvcolumnsum($filename,$col) {
    $handle2 = fopen($filename, 'r');
    $data = fgetcsv($handle2);

    foreach ($data as $headercolumn) {}
    while ($data = fgetcsv($handle2)) {
        $sum += $data[$col];
    }

    echo "&lt;span&gt;$sum&lt;/span&gt;";

    fclose($handle2);
}
</code></pre>
