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 6722407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:26:37+00:00 2026-05-26T09:26:37+00:00

This is a totally experimental question, but if answered it will save me hours

  • 0

This is a totally experimental question, but if answered it will save me hours of manual HTML mark up.

In theory it should work, but can appreciate advice if I’m talking rubbish.

I need a loop to pull column data from columns in a CSV spreadsheet, and echo them in HTML mark up.

I can’t write PHP but this is how I envisage the loop work…

<?php

    // here it needs to load the CSV file and get the column data and output them as variables (I guess)

echo <div id="interactive-map">

    // here a loop needs to begin to output this line off HTML...
    // with the arrayed variables...

<div id="[varible-1]" class="[varible-2]" title="[varible-3]"><span>[varible-3]</span></div>

    // loop finished once no more rows left in CSV

echo </div>

?>

So the result should look like this…

<div id="interactive-map">

    <div id="1" class="free" title="Uber"><span>Uber</span></div>
    <div id="2" class="free" title="Howdy"><span>Howdy</span></div>
    <div id="3" class="free" title="Love"><span>Love</span></div>
    <div id="4" class="free" title="Gimme"><span>Gimme</span></div>
    <div id="5" class="free" title="Totally"><span>Totally</span></div>
    <div id="6" class="free" title="Spank"><span>Spank</span></div>

</div>

The CSV files looks like this…


(source: motocom.co.uk)

Any help or advice would be TRULY amazing! Thanks

// UPDATE BELOW FOLLOWING FIRST ANSWER

My CSV below viewed as text…

id,class,name
1,free,Uber
2,free,Howdy
3,free,Love
4,free,Gimme
5,free,Totally
6,free,Spank

The PHP below…

    <?php

        $file = fopen('file.csv', 'r');
        $fields = array();

        if ($file) {
        while (($data = fgetcsv($file)) !== false) {

            if(empty($fields)) {
                $fields = $data;
                    continue;
                }


            $row = array_combine($fields, $data);

            $output = sprintf("% is ID, % is CLASS, % is NAME",
                $row['id'],
                $row['class'],
                $row['name']);

            echo $output;

            }

        fclose($file);
    }

?>

It’s not quite working properly, what am I doing wrong?

With regards to adding the HTML, I put the mark up inside where the echoed text is and it gets confused :-/

It is echoing stuff but not the desired info from the csv.

  • 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-05-26T09:26:37+00:00Added an answer on May 26, 2026 at 9:26 am

    To read the file, the easiest is to use the built-in fgetcsv in a loop. You can cook up your own parsing code, but it’s really thankless work to make it behave correctly in the presence of field delimiters and escaped characters.

    After reading the names of the CSV fields (first iteration) and their values for each row (subsequent iterations), you can use sprintf or vsprintf to easily construct an HTML string to output.

    For example:

    $file = fopen('php://stdin', 'r'); // or open any other file you want
    $fields = array(); // this holds the name of the fields, read from the 1st row
    
    if ($file) {
        while (($data = fgetcsv($file)) !== false) {
            // If this is the first row, we assume it holds field names.
            // So just remember what they are and loop to the next.
            if(empty($fields)) {
                $fields = $data;
                continue;
            }
    
            // Subsequent rows are assumed to contain data.
            // array_combine associates the data in the current row with the field
            // names from the first row, allowing us to refer to them using those
            // names and be independent of the order the fields appear in the input.
            $row = array_combine($fields, $data);
    
            // Format output conveniently with sprintf
            $output = sprintf("%s is %d years old.\n",
                              $row['name'],
                              $row['age']);
            echo $output;
        }
        fclose($file);
    }
    

    See it in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be totally simple but I can't get it working no matter what
This method is working totally right in matlab. but, when I compiled it in
Not totally sure how to approach this. I've researched a bit but I've come
Firstly: I am totally a newbie for this kind of work. I have a
I'm not totally sure but this looks wrong: I have a header file named
Update: After some more reading I see that this problem is totally general, you
Totally confused by this one... We have a WAMPServer installation set up, running a
This is so incredibly basic that I am totally baffled as to why it
I'm totally confused on this matter, some say use tables and others say it's
I'm totally stuck and have no idea how to go about solving this. Let's

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.