2016-05-06 4 views
1

私はphpで私に多次元配列を提供する関数を作っていましたが、これらのデータをアンドロイドに送信するにはjsonの応答に変換する必要がありますこれらの機能によりphparrayからjsonarrayへの型変換と応答の返信

{ 
    "": { 
      "0": "17", 
      "id": "17", 
      "1": "\"12:30\"", 
      "starttime": "\"12:30\"", 
      "2": "9", 
      "classid": "9", 
      "3": "\"2:30\"", 
      "endtime": "\"2:30\"", 
      "4": "cs602", 
      "lecturecode": "cs602", 
      "5": "1102", 
      "teacherid": "1102", 
      "6": "0827cs131235", 
      "enrollmentnumber": "0827cs131235", 
      "7": "\"P\"", 
      "status": "\"P\"", 
      "8": "\"24/04/2016\"", 
      "dateof": "\"24/04/2016\"", 
      "9": "\"sunday\"", 
      "dayof": "\"sunday\"", 
      "10": "2", 
      "lecturenumber": "2" 
    }, 
    "1": { 
      "0": "18", 
      "id": "18", 
      "1": "\"12:30\"", 
      "starttime": "\"12:30\"", 
      "2": "9", 
      "classid": "9", 
      "3": "\"2:30\"", 
      "endtime": "\"2:30\"", 
      "4": "cs602", 
      "lecturecode": "cs602", 
      "5": "1102", 
      "teacherid": "1102", 
      "6": "0827cs131236", 
      "enrollmentnumber": "0827cs131236", 
      "7": "\"P\"", 
      "status": "\"P\"", 
      "8": "\"24/04/2016\"", 
      "dateof": "\"24/04/2016\"", 
      "9": "\"sunday\"", 
      "dayof": "\"sunday\"", 
      "10": "2", 
      "lecturenumber": "2" 
    } 
} 
を生成されたデータは、

public function getabsendstudents($lecture1, $lecture2) { 

    function bool2str($bool) { 
     if ($bool === false) 
      return 'FALSE'; 
     else 
      return 'TRUE'; 
    } 

    function compareObjects(&$o1, &$o2) { 
     echo 'o1 == o2 : ' . bool2str($o1 == $o2) . "\n"; 
    } 

    ///queries 
    $ss = "SELECT * FROM attendence WHERE lecturenumber = $lecture1"; 
    $ss2 = "SELECT * FROM attendence WHERE lecturenumber = $lecture2"; 

    $result = mysql_query($ss); 
    $result2 = mysql_query($ss2); 

    // fetching number of rows 
    $no_of_rows = 0; 
    $no_of_rows2 = 0; 
    $no_of_rows = mysql_num_rows($result); 
    $no_of_rows2 = mysql_num_rows($result2); 

    ///// array objects 
    $testforstudent = array(); 
    $testforstudent2 = array(); 
    $absentstudent = array(); 
    $presentstudent = array(); 

    //// variable declaration 
    $len=0; 
    $result_array_for_lec1[] = array(); 
    $result_array_for_lec2[] = array(); 

    ///fetching whole students in arrays 
    if ($no_of_rows > 0) { 
     $acount = 0; 
     $pcout = 0; 

     while ($row = mysql_fetch_array($result)) { 
      $testforstudent[] = $row; 
      $acount = $acount + 1; 
     } 
    } 

    if ($no_of_rows > 0) { 
     $pcount = 0; 
     while ($row2 = mysql_fetch_array($result2)) { 
      $testforstudent2[] = $row2; 
      $pcount = $pcount + 1;   
     } 
    } 

    /// here we are comparing in status of student 
    // otherwise student had bunked classes   
    if ($no_of_rows > $no_of_rows2) 
     $len = $no_of_rows2; 
    else 
     $len = $no_of_rows; 

    global $p; 
    global $a; 
    for ($i = 0; $i < $len; $i++) { 
     $bo = strtolower($testforstudent[$i]["status"]) == strtolower($testforstudent2[$i]["status"]); 

     if ($bo) { 
      ////present student  
     } 
     else { 
      $absentstudent[$a] = $testforstudent2[$i]; 
      $a++; 
     } 
    } 

    echo json_encode($absentstudent); 
    return $absentstudent; 
} 

:今、私は様々な問題

にPHPの配列名がgetabsendstudentで提供している今、これらの機能を持って多くのことを試してみました問題はただのforeachのロジックを適用することで解決しました:

$result_array = $db->getabsendstudents($lecture1, $lecture2); 
if ($result_array) { 
    $count = 0; 

    // result array is catching absentstudent array 
    $response["success"] = 1; 
    $response["user"][$count]["classid"] = $result_array[$count]["classid"]; 

    $count = $count + 1; 
} 
else { 
    // user failed to store 
    $response["error"] = 1; 
    $response["error_msg"] = "JSON Error occured in Registartion"; 
    echo json_encode($response); 
} 
+0

です。else宣言の外側に 'echo json_encode($ response);'を呼び出すようにしてください。 – jaolstad

+0

私は既にそのことをしていました。 –

答えて

0

が解決される$をキャッチされ

今私が作成した1つのより多くの機能が$の変数でabsentarray resultarray.and私は、これらの機能 コードでコードが必要ですループとif($ result_array)の選択です。コードは

if ($result_array) { 
       $count=0;  
     foreach ($result_array as $value) 
      { 

       // user stored successfully 
      $response["success"] = 1; 
      $response["user"][$count]["classid"] = $value["classid"]; 
      $response["user"][$count]["enrollmentnumber"]= $value["enrollmentnumber"]; 
      $response["user"][$count]["teacherid"]=$value["teacherid"]; 


      $count=$count+1; 

      } 

    echo json_encode($response); 

    } 
関連する問題