Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. JSON Ajax PHP MYSQL - multiple records into JSON
  2. if (mysql_num_rows($pquery) == 1) {
  3.    $result = mysql_fetch_assoc($pquery);
  4.  
  5.    $query_ht = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]'");
  6.    $ht_result = mysql_fetch_array($query_ht); // THIS WILL ALWAYS RETURN 3 ROWS
  7.  
  8.    //echo json_encode($result); //THis will always contain one row
  9.    //echo json_encode($ht_result); // THIS WILL ALWAYS contain 3 ROWS
  10.        
  11. //echo json_encode(array(
  12.        //'comp' => $result,
  13.        //'ht' => $ht_result)
  14.        // );
  15.        
  16. $query_htl = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]' AND type='L'");
  17.    $query_htg = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]' AND type='G'");
  18.    $query_htr = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]' AND type='R'");
  19.    $ht_resultl = mysql_fetch_assoc($query_htl);
  20.    $ht_resultg = mysql_fetch_assoc($query_htg);
  21.    $ht_resultr = mysql_fetch_assoc($query_htr);
  22.    echo json_encode(array(
  23.        'comp' => $result,
  24.        'htL' => $ht_resultl,
  25.        'htG' => $ht_resultg,
  26.        'htR' => $ht_resultr)
  27.         );
  28.  
  29. }
  30.        
  31. $('#c_search').submit(function(){
  32.        data = ($(this).serialize());
  33.  
  34.   $.ajax({
  35.       url: 'actions/get_company.php',
  36.       type: 'POST',
  37.       data: data,
  38.       cache: false,
  39.       dataType: 'json',
  40.       success: function(selected){
  41.        
  42. alert(selected.htL.id);
  43.        
  44. header('Content-type: application/json');
  45. die(json_encode(array(
  46.     'result'    => $result,
  47.     'ht_result' => $ht_result
  48. )));