0

I have a database with 5 columns and multiple rows. I'm using PHP to fetch the data and echo it as JSON as an array. The way it echoes it is the data in each row is an array and the rows themselves are an array (array within an array).

Now I bring the data in using Ajax and want to split the array within an array. I can only split down the rows so far. How would I split further?

Here's the PHP:

  //==== FETCH DATA
  $result = mysql_query("SELECT * FROM $tableName");

  //==== PUT DATA INTO ARRAY
  $array = array();
  while ($row = mysql_fetch_row($result)) {
    $array[] = $row;
  }

  //==== ECHO AS JSON
  echo json_encode($array);

Here's the Ajax:

$.ajax({                                      
  url: 'assets/php/results.php',    
  data: "",
  dataType: 'json',                  
  success: function(data){

    //==== FETCH DATA FIELDS
    var row1 = data[0];
    var row2 = data[1];

    //==== UPDATE HTML WITH DATA
    $('#r-col span.row1').html(row1);
    $('#r-col span.row2').html(row2);
  }
});
2
  • What does the json_encode'd data look like? Commented Mar 19, 2013 at 22:52
  • And what data do you expect ? Commented Mar 19, 2013 at 22:52

1 Answer 1

3

To answer your question, let me give you a few tips on how you can achieve this in shorter steps.
First off, try using msqli_ functions instead of msql_ (in php) it has a better handling of security among other improvements, and are also the functions w3schools recommend using.

There is a function called mysqli_fetch_array() that will do exactly what you are doing with your for loop in a single command.

And to answer your question on the json, you should take a look at what a json really looks like:

var data = {"menu": {
  "id": "file",
  "value": "File123",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

Json objects are not arrays, they are objects. That means I can access the information like this:

data.menu.id //Which is equal to "file"<br>

or

data[0][0]  //which is also equal to "file"<br>

or a combination

data[0].value  //is equal to "File123"

A great way to understand the way this objects work is by using the interactive console in Chrome. It has a pretty easy to understand printing of objects. I would also advice you to read some about object oriented programming on javascript.

In a nutshell: Json objects are not arrays, but you can sort through them easily by appending . or []

A small note: Try to avoid using Select * in your statements, they take a lot of resources, just call the fields you need by name.
Also, msql_ functions are a bit deprecated right now, try using msqli_ functions instead, they have several upgrades in the security side, you can check w3schools.com for examples.
Finally, there is a function in php called msqli_fetch_array that does exactly what you're trying to do with your for loop, it will convert your query output to an array.



Happy Coding.

1
  • YYEEEEESSSSS. You legendary person. This has been driving me nuts all night. Thank you. Commented Mar 19, 2013 at 23:21

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.