Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8006177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:25:34+00:00 2026-06-04T17:25:34+00:00

The code below gets a list of all the .csv files in a directory.

  • 0

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

The html output should look something like this:

<li>Filename (sum)</li>

But it comes out like this:

sum<li>Filename ()</li>

Can anyone point out why this is happening? Here is the relevant code.

require("csvcolumnsum.php");

echo "<ul class=\"users\">";
$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 "<li><a href=\"?form_name=$file\">$file(";
        echo $value;
        echo ")</a></li>";
    }
}
closedir($handle);
echo "</ul>";

csvcolumnsum.php

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

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

    echo "<span>$sum</span>";

    fclose($handle2);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:25:35+00:00Added an answer on June 4, 2026 at 5:25 pm

    That isn’t how echo works. If your want your function to communicate a value back to the calling function, your function needs to return the value, not echo it:

    return "<span>$sum</span>";
    

    Echo writes directly to the standard output, meaning this line:

    $value = csvcolumnsum("data/".$file.".csv",3);
    

    prints before these lines:

    echo "<li><a href=\"?form_name=$file\">$file(";
    echo $value;
    echo ")</a></li>";
    

    Returning halts execution in the current function, meaning your close will need to occur before your return:

    function csvcolumnsum($filename,$col) {
        $handle2 = fopen($filename, 'r');
    
        # ...
    
        fclose($handle2);
    
        return "<span>$sum</span>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below is the html code for TD which gets appended after matching
The code below gets the information i require from my database but is not
The code below gets the username/password and runs it thru the backend.php script. <?php
The code pasted below gets an exception on the line that says... InputStream in
Code below is working well as long as I have class ClassSameAssembly in same
In the code below ViewState[L] stores a List<string> . I create a new instance
i have used a checkbox list in my project .am storing all the checked
I am migrating some code and have nicely formatted Look-up Tables but they have
I have a code that fetches a list from DB. Using that returned array
What's missing in the following code, for the .Caption gets bellow and the icon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.