0

PHP: Code:

$category_searchresult = $db->db_query("SELECT category_code, category_name, category_comment FROM qa_categories WHERE ".$search_by." LIKE '%".$search_string."%'");

        $i=1;
        $qa = array();
        while($categories_query_result = mysql_fetch_array($category_searchresult))
        {
            $qa[$i][] = $categories_query_result['category_code'];
            $qa[$i][] = $categories_query_result['category_name'];
            $qa[$i][] = $categories_query_result['category_comment'];

            $i++;
         }
        echo json_encode($qa);

JS Code:

$.ajax({
     type: "POST",
     url: "ajax_js.php",
     data: search_data,
     cache: false,
     success: function(search_result) {
        //Print Here
    });

Current Output IS from PHP:

{"1":["4BA3CC","Fontaneria","Para la Casa"],"2":["CF0345","Herramientas","Herramients de Hogar"],"3":["1265CA","Luces","Luces de todo tipo"],"4":["4C4C9F","Vidrios","Reflectores de auto"]}

Many Thank's

3
  • 2
    What do you mean by "Print" -- what exactly do you want to do? Commented Aug 19, 2011 at 21:10
  • See stackoverflow.com/questions/4933217/print-json-parsed-object Commented Aug 19, 2011 at 21:12
  • When i say print, i tell to say ECHO the array, i have an JSON ENCODED string and i need to DECODE and ECHO using JS Commented Aug 19, 2011 at 21:15

2 Answers 2

2

Add a

dataType: 'json'

to your .ajax() call. That will tell jQuery to decode the JSON string from PHP back into a native Javascript data structure. Alternatively, you can take the data parameter in the success handler and explicitly do the decoding yourself:

success: function(data) {
    var data = jquery.parseJSON(data);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this realy help me :) but is: parseJSON(data);
1

Not quiet sure what the output should be but this is the code you can use to traverse a json object:

for(var key in search_result) {
   var result = search_result[key];
   //result is an array, so you can traverse or access each value by the key
   // or you can join all the values together
   var joinedValues = result.join(' | ');
   //joinedValues will be something like '4BA3CC|Fontaneria|Para la Case'
}

You may want to set those values into a container object in the page to diplay them of course. Hope this helps.

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.