0

as the title stated. how do i do that? i read in some forum to use stringify? and how do i use it?

PHP:

$notes = $db->query("SELECT * FROM notes WHERE id_user = '$id_user'");
$output = array();
while($note = $notes->fetch_assoc()) {
  $noting = '<span class="block_note">'.$note['note'].'</span>';
  array_push($output, $noting);
}
$data['ok'] = true;
$data['message'] = $output;
echo json_encode($data);

jQuery:

$.get("include/get_details.php?id_user=1",function(data){
  if (data.ok){
     $.each(data, function(key,id){
        $("#notes").append(data.message);
     })

  }
}, "json");

Output:

{
"ok":true,
"message":[
  "<span class=\"block_note\">note1<\/span>",
  "<span class=\"block_note\">note2<\/span>",
  "<span class=\"block_note\">note3<\/span>"
]
}
1
  • I don't get this question. By using json_encode you (in a way) convert a array to a string (or object). How to you want to use this? Where is the error? Commented Nov 1, 2011 at 11:55

5 Answers 5

2
$.get("include/get_details.php?id_user=1",function(data){
  if (data.ok){
     $.each(data.message, function(key,value){
        $("#notes").append(value);
     })

  }
}, "json");
Sign up to request clarification or add additional context in comments.

1 Comment

thanks.. i tried ur way before for some reason it didnt work.. and its working fine. weird.. lol.. thanks alot :)
1

array.join(separator)

In you case:

message = data.message;
html = message.join('');

Comments

1

I believe the easiest way to do this would be to use the jQuery .toArray() function, and then javascript join().

Like this:

var myArray = $(".block_note").toArray();
myArray.join("");

Comments

1
$.get("include/get_details.php?id_user=1",function(data){
  if (data.ok){
     $.each(data.message, function(key,id){
     $("#notes").append(id);
    });
  }
}, "json");

Check out the fiddle here http://jsfiddle.net/ryan_s/58cLY/

Comments

1

data.message is a JavaScript Array, so you can just use the Array.join method:

...
if (data.ok){
    $("#notes").append(data.message.join(""));
}
...

The Array object also provides various iteration methonds which you should find useful.

PS: you can also write your PHP code like this:

$data = array();
$data['message'] = array();
while($note = $notes->fetch_assoc()) {
    $data['message'][] = '<span class="block_note">' . $note['note'] . '</span>';
}
$data['ok'] = true;

Comments

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.