Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
  }
});
share|improve this question
What does the json_encode'd data look like? – castis Mar 19 at 22:52
And what data do you expect ? – MatRt Mar 19 at 22:52

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
YYEEEEESSSSS. You legendary person. This has been driving me nuts all night. Thank you. – Coop Mar 19 at 23:21
No biggie, you're welcome. I'm glad it's useful ^^ – Ryoku Mar 19 at 23:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.