2016-01-19 8 views
5

私はyearselectionに2つのテーブルがあり、2つのカラムがあり、testtable2の3つのカラムがあります。第2のテーブルで使用している最初のテーブルIDに基づいて、以下のようにphpを使用してjson応答を表示します。jsonのレスポンスはどのようにphpの下のフォーマットから取得できますか?

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 

私は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

どのようにあなたのPHPコードを見て?それを共有してください。 –

+0

まず、このクエリを試してみてください。 は 'ys.yearを選択、tt.id、yearselectionのYS 左からtt.nameがys.id BY testtable2 TT ON tt.yearselection = ys.id ORDERを登録しよう;' –

+0

は、上記の私のPHPコードを追加しました。 – sairam

答えて

3

あなたは今、あなたは全体取るている今、あなたは上記のクエリを記述したら、あなたは列年、year_idを使用してデータを取得する、test_id、名前

最初にこの

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

のようなクエリを記述しています上記のように変数$arr[]に配列内のデータ(クエリデータを保存するには、$query変数には触れないでください)。

今、あなたは、単にjson_encode($arr);

行うことができます - あなたのループの長さが100と仮定するであるならば、それは DB 100回をヒットするよう

をループ内でDBをヒットしないでください。したがって、これらのケースでJOINSを適用して、 が1つのパラメータに基づいて2つのテーブルからデータを取得するようにすることができます。

希望すると、クエリが解決します。

+0

上記のJSON形式を表示するにはどうしたらいいですか? – sairam

+0

あなたはそれをテーブルや他の何かに表示したいと思っていますが、もしあなたがjsonにアクセスしたいのであれば、それもできます.... – Abbas

+0

私は印刷するそうですね。 – sairam

関連する問題