2016-01-19 4 views
5

ho due tabelle, in yearselection ci sono due colonne e in testtable2 ci sono tre colonne, in base all'ID della prima tabella che utilizza nella seconda tabella., Voglio visualizzare la risposta di Json come sotto usando php, queste due tabelle.come posso ottenere risposta JSON da PHP sotto il formato?

yearselection:

id year 
    6 2014-2015 
    2 2010-2011 
    3 2011-2012 
    4 2012-2013 
    5 2013-2014 
    1 2009-2010 
    7 2015-2016 

testtable2:

id name yearselection 
1 test1  2 
2 test2  1 
3 test3  1 
4 test4  1 
5 test5  2 
6 test6  3 

voglio mostra come questo in formato JSON:

{ 
    "2009-2010": [ 

     { 
      "id": "2", 
      "name": "test2" 
     }, 

     { 
      "id": "3", 
      "name": "test3" 
     }, 

     { 
      "id": "4", 
      "name": "test4" 
     } 

    ], 
    "2010-2011": [ 

     { 
      "id": "1", 
      "name": "test1" 
     }, 

     { 
      "id": "5", 
      "name": "test5" 
     } 

    ], 
    "2011-2012": [ 

     { 
      "id": "6", 
      "name": "test6" 
     } 

    ] 
} 

mycode

public function actionArchives() 
    { 
    //echo $keyword=$_POST['keyword']; 

     $query= Yii::app()->db->createCommand("select * from yearselection ORDER BY id ASC")->queryAll(); 
     $arr = array(); 
     if(count($query) > 0) { 

     foreach ($query as $queryElement) { 

     $query2= Yii::app()->db->createCommand("select * from testtable2 where yearselection='".$queryElement['id']."' ORDER BY id ASC")->queryAll(); 




     $arr[] = $queryElement; 
     } 
     } 

     # JSON-encode the response 
     $json_response = json_encode($arr); 

     // # Return the response 
     echo $json_response; 


    //exit; 
    } 
+0

Come sembra il codice PHP? Per favore, condividilo. –

+0

Prima prova questa query: 'SELECT ys.year, tt.id, tt.name FROM yearelection ys LEFT JOIN testtable2 tt ON tt.yearselection = ys.id ORDINA DA ys.id;' –

+0

aggiunto il mio codice php sopra . – sairam

risposta

3

avete scrivere la query prima come questo

public function actionArchives() 
{ 
//echo $keyword=$_POST['keyword']; 

    $query= Yii::app()->db->createCommand("SELECT y.year,y.id as year_id,t.id as test_id,t.name 
    FROM yearselection as y JOIN testtable2 as t 
    ON y.id=t.yearselection")->queryAll(); 
    $arr=array(); 
    if(count($query) > 0) 
    { 
     $arr = $query; 
    // Now you run loop to change indexes of array,i.e. 
     $count=count($arr); 
     for($i=0;$i<$count;$i++) 
     { 
     $year=$arr[$i]['year']; 
     $arr[$year]=$arr[$i]; 
     unset($arr[$i]); 
     } 
    // Now your array has index as your year column, 
    } 

    # JSON-encode the response 
    $json_response = json_encode($arr); 

    // # Return the response 
    echo $json_response; 


//exit; 
} 

Ora, una volta che si scrive la query di cui sopra si ottengono i dati con le colonne anno, year_id, test_id, nome

Ora avete prendere tutto dati in un array come sopra nella variabile $arr[] (non si tocca la variabile $query per conservare i dati della query).

Ora si può fare semplicemente json_encode($arr);

Nota: -

Si prega di non colpire DB in un ciclo, come se la lunghezza del ciclo è supponiamo 100 allora colpirà DB 100 volte . Quindi puoi applicare JOINS in questi casi in modo che prenderà i dati da 2 tabelle in base a un singolo parametro.

Spero che questo risolva la tua richiesta.

+0

come posso visualizzare sopra il formato json. – sairam

+0

dove vuoi visualizzarlo cioè in tabella o qualsiasi altra cosa però e se vuoi accedere a json allora puoi anche farlo .... – Abbas

+0

voglio solo dare una chiamata api per android sopra il formato json, voglio solo stampare come quello. – sairam