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

blackriderws
  • 833
  • 2
  • 9
  • 14

2 Answers2

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);
}
Chris Hepner
  • 1,552
  • 9
  • 16
Marc B
  • 356,200
  • 43
  • 426
  • 500
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.

theprogrammer
  • 2,724
  • 1
  • 18
  • 13