如何从多个mysql表中获取数据并生成一个JSON输出

时间:2015-01-30 17:16:00

标签: php mysql json

我有这个PHP代码:

 try {
      $result = $db->prepare('SELECT ID,masina,marka,model,reg_br,istek_reg,servis FROM mehanizacija WHERE user_id=:user_id');
      $result->bindParam(':user_id', $user_id); 
      $result->execute();

        foreach($result as $r) {
          $temp = array();
          $temp[] = (int) $r['ID']; 
          $temp[] = (string) $r['masina'];
          $temp[] = (string) $r['marka']; 
          $temp[] = $r['model'];
          $temp[] = (string) $r['reg_br'];
          $temp[] = 'Date('.str_replace('-',', ',($r['istek_reg'])).')';
          $temp[] = 'Date('.str_replace('-',', ',($r['servis'])).')';

        $rs = $db->prepare('SELECT sum(radnih_sati) FROM track_meh WHERE user_id=:user_id AND id_meh=:id_meh');
          $rs->bindParam(':user_id', $user_id); 
          $rs->bindParam(':id_meh', $r['ID']); 
          //$rs->bindParam(':radnici', $radnici); 
          $rs->execute();
          $sumRows = $rs->fetchColumn(); 
          $temp[] = array('v' => (int) $sumRows);
 $rs1 = $db->prepare('SELECT sum(kolicina) FROM track_meh WHERE user_id=:user_id AND id_meh=:id_meh');
          $rs1->bindParam(':user_id', $user_id); 
          $rs1->bindParam(':id_meh', $r['ID']); 
          $rs1->execute();
          $sumRows1 = $rs1->fetchColumn(); 
          $temp[] = array('v' => (int) $sumRows1);
          $prosecna = $sumRows1/$sumRows;
          $temp[] = array('v' => (float) $prosecna. ' l/h');

          $rows[] = $temp;
        }
    $table['data'] = $rows;
    $jsonTable = json_encode($table);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    echo $jsonTable;

现在我有了这个JSON输出:

{"data":[[1,"traktor","IMT","510-td","BG-657 UI","Date(2014, 04, 24)","Date(2014, 02, 18)",53,95,"1.79245283019 l\/h"],[3,"Vrsalica","John Deer","n7","BG 777 hb","Date(2014, 07, 18)","Date(2014, 01, 20)",24,66,"2.75 l\/h"],[4,"prikolica","nema","nema","","Date(0000, 00, 00)","Date(0000, 00, 00)",0,0,"0 l\/h"],[5,"traktor","","","","Date(0000, 00, 00)","Date(0000, 00, 00)",0,0,"0 l\/h"],[6,"kombajn","Novi","NN","Pi 001 ZN","Date(0000, 00, 00)","Date(0000, 00, 00)",24,50,"2.08333333333 l\/h"],[7,"traktor","mnm","","","Date(0000, 00, 00)","Date(0000, 00, 00)",6,9,"1.5 l\/h"],[8,"traktor","","","","Date(0000, 00, 00)","Date(0000, 00, 00)",0,0,"0 l\/h"],[9,"alat","","","","Date(0000, 00, 00)","Date(0000, 00, 00)",0,0,"0 l\/h"],[10,"traktor","","","","Date(0000, 00, 00)","Date(0000, 00, 00)",0,0,"0 l\/h"],[11,"setvo-spremac","","","","Date(0000, 00, 00)","Date(0000, 00, 00)",0,0,"0 l\/h"],[12,"traktor","","","","Date(0000, 00, 00)","Date(0000, 00, 00)",0,0,"0 l\/h"]]}

我需要做什么才能将我的JSON输出格式转换为:

{"data":[{"ID":"1","masina":"traktor","marka":"IMT","model":"510-td","reg_br":"BG-657 UI","istek_reg":"2014-04-24","servis":"2014-02-18","sumRows":"555", "sumRows1":"333","prosecna":"2.0999"}, etc ...

因为你看到我的JSON输出是错误的,因为dataTable插件需要像我上面那样的格式数据,因为我需要找到一种方法来按照以下格式转换数据。

知道怎么做吗?我尝试了很多不同的方法,但我无法解决这个问题

1 个答案:

答案 0 :(得分:1)

$temp[]的使用给你一个数字键控数组,这就是最终结果是数组数组的原因。根据您所需的输出,您需要一组关联数组,这些数组最终会在JSON转换后为您提供一个对象数组(作为字符串)。

使用相关密钥将每个$temp[]替换为$temp['some_key']

$temp['ID'] = (int) $r['ID']; 
$temp['traktor'] = (string) $r['masina'];