1

I am using this code to process an uploaded csv file. How do I convert it to Json and loop through to display on a user's screen?

 if (($handle = fopen('upload/'.$current.$_FILES["file"]["name"]. '', "r")) !== FALSE) {
    while (($row_array = fgetcsv($handle, 1024, ","))) { 
         foreach ($row_array as $key => $val) {
             $row_array[$key] = trim(str_replace('"', '', $val));
             }
         $complete[] = $row_array;
         }
         fclose($handle);
     }
3
  • We'd have to know what object is represented by the CSV data. Also, we'd have to explain how to do an ajax call and convert the results to HTML. That's going to require more space than we have here. Break your question into smaller parts. Commented May 21, 2012 at 20:15
  • Why do you need JSON at all? As soon you have parsed the file, you can print it in the HTML page. Commented May 21, 2012 at 20:25
  • Because I need to display the data with the option to make changes via drop-downs for each row. And then once the user makes the appropriate changes, I then need to convert it into an XML. Commented May 21, 2012 at 20:35

1 Answer 1

1

You should encode your array to json using

json_encode($complete);

Then you will need to iterate that array using javascript on client side.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you show how to iterate the array and use javascript to display?
You can read more about traversing json structure here: stackoverflow.com/questions/1078118/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.