1

I have 3 Tables from mysql database and I need to combine them to get one JSON output...

With first table is easy to get JSON output so I write:

 try {

          $result = $db->prepare('SELECT datum,vrsta_prodaje,artikl,bruto_kol,jmere,skladiste FROM prodaja WHERE user_id=:user_id');
          $result->bindParam(':user_id', $user_id); 
          $result->execute();


            foreach($result as $r) {
              $temp = array();
                  $temp['datum'] = (string) $r['datum'];
              $temp['vrsta'] = (string) $r['vrsta_prodaje'];
              $temp['artikl'] = (string) $r['artikl']; 
              $temp['6'] = (int) $r['bruto_kol'].' '.$r['jmere'];
              $temp['jmere'] = (string) $r['jmere'];
              $temp['skladiste'] = (string) $r['skladiste']; 
//here I will add empty json becouse on second table I have one more column data
              $temp['id_parcele'] = (string) ''; 
              $rows[] = $temp;
            }
        $table['data'] = $rows;
    $jsonTable = json_encode($table);

        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
        echo $jsonTable;

but I need to combine two more tables track_djub and track_berba here

from track_djub I must get:

SELECT datum,vrsta,stavka,kolicina,jmere,skladiste,id_parcele FROM track_djub WHERE user_id=:user_id and

from track_berba I must get:

SELECT datum,stavka,prinos,jmere,skladiste,id_parcele FROM track_berba WHERE user_id=:user_id

after all this I need to order by datum (date)

Please give me some ideas how I can combine this 3 tables into one JSON output ordered by DATUM

How I can do this?

UPDATE: WHAT I TRY TO DO: BUT DONT GET ANY DATA:

try {

      $result = $db->prepare('SELECT datum,vrsta_prodaje,artikl,bruto_kol,jmere,skladiste FROM prodaja WHERE user_id=:user_id');
      $result->bindParam(':user_id', $user_id); 
      $result->execute();


        foreach($result as $r) {
          $temp = array();
              $temp['datum'] = (string) $r['datum'];
          $temp['vrsta'] = (string) $r['vrsta_prodaje'];
          $temp['artikl'] = (string) $r['artikl']; 
          $temp['6'] = (int) $r['bruto_kol'].' '.$r['jmere'];
          $temp['jmere'] = (string) $r['jmere'];
          $temp['skladiste'] = (string) $r['skladiste']; 
                  $temp['id_parcele'] = '';
          $rows[] = $temp;
        }

        $result1 = $db->prepare('SELECT datum,vrsta,stavka,kolicina,jmere,id_parcele FROM track_djub WHERE user_id=:user_id');
      $result1->bindParam(':user_id', $user_id); 
      $result1->execute();

        foreach($result1 as $a) {
          $temp1 = array();
              $temp1['datum'] = (string) $a['datum'];
          $temp1['vrsta'] = (string) $a['vrsta'];
          $temp1['artikl'] = (string) $a['stavka']; 
          $temp1['6'] = (int) $a['kolicina'].' '.$a['jmere'];
          $temp1['jmere'] = (string) $a['jmere'];
          $temp1['skladiste'] = (string) ''; 
                  $temp1['id_parcele'] = (int) $a['id_parcele']; 

          $rows1[] = $temp1;
        }

          $result2 = $db->prepare('SELECT datum,kultura,prinos,jmere,id_parcele FROM track_berba WHERE user_id=:user_id');
      $result2->bindParam(':user_id', $user_id); 
      $result2->execute();

        foreach($result2 as $b) {
          $temp2 = array();
              $temp2['datum'] = (string) $b['datum'];
          $temp2['vrsta'] = (string) 'Kultura';
          $temp2['artikl'] = (string) $b['kultura']; 
          $temp2['6'] = (int) $b['prinos'].' '.$b['jmere'];
          $temp2['jmere'] = (string) $b['jmere'];
          $temp2['skladiste'] = (string) ''; 
                  $temp2['id_parcele'] = (int) $b['id_parcele']; 

          $rows2[] = $temp2;
        }


    $table['data'] = $rows.$rows1.$rows2;

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    echo $jsonTable;
johny mile
  • 145
  • 1
  • 2
  • 11

0 Answers0