2016-01-19 15 views
5

İki tablo var, yearselection içinde iki sütun var ve testtable2 içinde üç sütun var, ikinci tablodaki ilk tablo kimliğine göre. Jm responce'ı aşağıdaki gibi bu iki tabloyu kullanarak görüntülemek istiyorum.jp responce'ı aşağıdaki formattan nasıl alabilirim?

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 

i json formatında böyle ekranı istiyorum:

{ 
    "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" 
     } 

    ] 
} 

BBKod

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

PHP kodunuza nasıl bakıyorsunuz? Lütfen paylaş. –

+0

Öncelikle bu sorguyu deneyin: ys.id TARAFINDAN yearselection ys SOL tt.yearselection = ys.id SİPARİŞ ÜZERİNE testtable2 tt JOIN GELEN 'SEÇ ys.year, tt.id, tt.name;' –

+0

yukarıda benim php kodu eklendi . – sairam

cevap

3

sen sütunlar yıl, year_id, test_id, isim ile verilerinizi alacak Yukarıdaki sorgu yazmak kez Şimdi

Eğer bütün aldırmak Şimdi öncelikle bu

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; 
} 

gibi sorgu yazmak zorunda $arr[] değişkeninde yukarıda yaptığınız gibi bir dizideki veriler (sorgu verilerinin korunması için $query'a dokunmamaya dikkat edin). Şimdi

Yapabileceğiniz sadece json_encode($arr);

Not: -

sizin döngü uzunluğu o zaman DB 100 kez vuracaktır 100 varsayalım sanki bir döngüde DB vurma Lütfen . Bu nedenle, bu durumlarda JOINS'i uygulayabilirsiniz, böylece , tek bir parametreye dayanarak 2 tablodan veri alır.

Sorunuzu çözdüğünü umuyorum.

+0

json formatının üstünde nasıl görüntüleyebilirim. – sairam

+0

nerede göstermek istediğiniz yani tablo veya başka bir şey olsa ve json erişmek istiyorsanız o zaman da yapabilirsiniz .... – Abbas

+0

sadece json formatında android için api çağrı vermek istiyorum, sadece ben yazdırmak istiyorum bunun gibi. – sairam

İlgili konular