0

I am using data that's in a csv file to display it using JSON. I can see it in the alert, but not in the html. Also, I tried to create an associative array to make it easier to refer to through Jquery, but it's not being passed that way.

 <?php 
 if (($handle = fopen('upload/05-22-2012-1-43-28BEN-new.csv'. '', "r")) !== FALSE) {
       while (($row_array = fgetcsv($handle, 1024, ","))) {
            while ($val != '') {
                foreach ($row_array as $key => $val) {
                        $row_array[] = array_combine($key, trim(str_replace('"', '', $val)));
                        }
                }
            $complete[] = $row_array;
            //print_r($row_array);

            }
            fclose($handle);
        }
        echo json_encode($complete);

 ?>

HTML

 <body>    
      <div id="showdata"></div>

 </body>

JQuery

 $(document).ready(function(){
    $.getJSON('WF-XML.php', function(data) {
        alert(data); //uncomment this for debug
        //alert (data.item1+" "+data.item2+" "+data.item3); //further debug
        $('#showdata').html(data);
    });
});

Results

 [["11704","1611704","BENV1072"],["11703","1611703","BENV1073"]]
4
  • Please read the jQuery docs for what argument type to pass into the html() method. Commented May 22, 2012 at 20:41
  • Also, I have no idea why you call array_combine with two strings. Do you? Commented May 22, 2012 at 20:44
  • I was trying to create an associative array. Commented May 22, 2012 at 20:46
  • Is $row_array not already associative, as you loop through it with a foreach-as-loop? Commented May 22, 2012 at 20:56

1 Answer 1

1

First of all you need to stringify your object using JSON.stringify, also .html() method does not escape the string - try using .text() instead.

I've created a simple fiddle for testing - http://jsfiddle.net/E9KTy/

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

5 Comments

Thanks! Thank worked! How would I loop through the results and display them in an html formatted way?
Sorry, not sure what you mean. Could you please update your post and add an example JSON string that I can use for testing?
Also, my results are being displayed like this: [["11704","1611704","BENV1072"],["11703","1611703","BENV1073"]] Without the sqwiggled brackets. How would I refer to them in an array?
OK, so basically you have a two dimensions array and want to loop through the second dimension? If this is the case then check out the following example - jsfiddle.net/E9KTy/2
What if I only wanted to print the data that's in the second position of the array?

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.