0

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);
     }
MG1
  • 1,655
  • 9
  • 33
  • 46
  • 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. – Jonathan M May 21 '12 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. – Bergi May 21 '12 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. – MG1 May 21 '12 at 20:35

1 Answers1

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.

svlada
  • 3,218
  • 2
  • 25
  • 36
  • Can you show how to iterate the array and use javascript to display? – MG1 May 21 '12 at 20:38
  • You can read more about traversing json structure here: http://stackoverflow.com/questions/1078118/how-to-iterate-over-a-json-structure – svlada May 22 '12 at 11:39