2017-03-06 27 views
0

PHPを使用してmysqlからいくつかのデータにアクセスし、JSON文字列としてAJAXリクエストを介してJavaスクリプトに送信したいと考えました。しかし、問題は、Javaスクリプトで文字列を解析した後、私はデータを使用することができません。 PHPコードは、クラスの配列をJSON文字列に変換して送信しています。もしそうなら、適切なやり方を教えてください。JavaScriptでJSONデータをデコードできない

PHPコードは次のとおり

<?php 
      ini_set('error_reporting', E_STRICT);  //Suppress warnings. !!Disable only during developmental time 
      $servername = "localhost"; 
      $username = "root"; 
      $password = ""; 
      $dbname = "crie_test"; 

      //create connection 
      $conn = new mysqli($servername,$username,$password,$dbname); 
      if($conn->connect_error){ 
       die("Connection failed".$conn->connect_error); 
      } 

      class publications{ 
       public $dept_name,$dept_code,$int_jour, $nat_jour, $inter_nat_conf, $nat_conf, $int_book_chap, $nat_book_chap; 
      } 

      //Fetch department codes 
      $sql = "SELECT * FROM tbl_dept_info"; 
      $res = $conn->query($sql); 
      $no_of_dept = mysqli_num_rows($res); 

      $dept_li[$no_of_dept] = new publications();   //create department object array 

      //Intialize variables 
      for($i=0;$i<$no_of_dept;$i++){ 
       $dept_li[$i]->int_jour=0; 
       $dept_li[$i]->nat_jour=0; 
       $dept_li[$i]->inter_nat_conf=0; 
       $dept_li[$i]->nat_conf=0; 
       $dept_li[$i]->int_book_chap=0; 
       $dept_li[$i]->nat_book_chap=0; 
      } 

      if ($res->num_rows > 0) { 
       $i = 0; 
       while($row = $res->fetch_assoc()) { 
        $dept_li[$i]->dept_code = $row["Dept_Code"]; 
        $dept_li[$i]->dept_name = $row["DeptType"]; 
        $i++; 
       } 
      } 
      else{ 
       echo "Unable to fetch department id's!!"; 
      } 

      //fetch the research table 
      $sql1 = "SELECT tbl_dept_research.ResearchCode, tbl_dept_research.DeptCode, tbl_research.Publication FROM tbl_dept_research INNER JOIN tbl_research ON tbl_dept_research.ResearchCode=tbl_research.ResearchCode"; 
      $res1 = $conn->query($sql1); 


      if($res1->num_rows>0){ 
       while($row = $res1->fetch_assoc()) { 
        $dep_code = $row["DeptCode"]; 
        for($i=0;$i<$no_of_dept;$i++){ 
         if($dept_li[$i]->dept_code==$dep_code){ 
          switch($row['Publication']){ 
           case"International Journal": $dept_li[$i]->int_jour++; 
                  break; 
           case"National Journal": $dept_li[$i]->nat_jour++; 
                  break; 
           case"International Conference": $dept_li[$i]->inter_nat_conf++; 
                  break; 
           case"National Conference": $dept_li[$i]->nat_conf++; 
                  break; 
           case"Book Chapter International": $dept_li[$i]->int_book_chap++; 
                  break; 
           case"Book Chapter National": $dept_li[$i]->nat_book_chap++; 
                  break; 
          } 
         } 
        } 
       } 
      } 

      echo json_encode($dept_li); 

      $conn->close(); 
     ?> 

クライアント側のスクリプトである:

<!DOCTYPE html> 
<html> 
<body> 

<h2>Get data as JSON from a PHP file on the server.</h2> 

<p id="demo"></p> 

<script> 

var xmlhttp = new XMLHttpRequest(); 

xmlhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     myObj = JSON.parse(this.responseText); 
     document.getElementById("demo").innerHTML = myObj.0.dept_name; 
    } 
}; 
xmlhttp.open("GET", "sss.php", true); 
xmlhttp.send(); 

</script> 

</body> 
</html> 

また、参考のために、このJSON文字列がPHPスクリプトによって生成されている:

{ 
    "8": { 
     "dept_name": null, 
     "dept_code": null, 
     "int_jour": null, 
     "nat_jour": null, 
     "inter_nat_conf": null, 
     "nat_conf": null, 
     "int_book_chap": null, 
     "nat_book_chap": null 
    }, 
    "0": { 
     "int_jour": 10, 
     "nat_jour": 1, 
     "inter_nat_conf": 16, 
     "nat_conf": 14, 
     "int_book_chap": 1, 
     "nat_book_chap": 4, 
     "dept_code": "101", 
     "dept_name": "ECE" 
    }, 
    "1": { 
     "int_jour": 22, 
     "nat_jour": 1, 
     "inter_nat_conf": 32, 
     "nat_conf": 16, 
     "int_book_chap": 5, 
     "nat_book_chap": 0, 
     "dept_code": "102", 
     "dept_name": "CSE" 
    }, 
    "2": { 
     "int_jour": 12, 
     "nat_jour": 4, 
     "inter_nat_conf": 10, 
     "nat_conf": 23, 
     "int_book_chap": 1, 
     "nat_book_chap": 0, 
     "dept_code": "103", 
     "dept_name": "IT" 
    }, 
    "3": { 
     "int_jour": 21, 
     "nat_jour": 0, 
     "inter_nat_conf": 9, 
     "nat_conf": 35, 
     "int_book_chap": 0, 
     "nat_book_chap": 0, 
     "dept_code": "104", 
     "dept_name": "EE" 
    }, 
    "4": { 
     "int_jour": 13, 
     "nat_jour": 1, 
     "inter_nat_conf": 8, 
     "nat_conf": 33, 
     "int_book_chap": 0, 
     "nat_book_chap": 1, 
     "dept_code": "105", 
     "dept_name": "MCA" 
    }, 
    "5": { 
     "int_jour": 10, 
     "nat_jour": 5, 
     "inter_nat_conf": 12, 
     "nat_conf": 13, 
     "int_book_chap": 0, 
     "nat_book_chap": 1, 
     "dept_code": "106", 
     "dept_name": "MBA" 
    }, 
    "6": { 
     "int_jour": 57, 
     "nat_jour": 6, 
     "inter_nat_conf": 5, 
     "nat_conf": 10, 
     "int_book_chap": 0, 
     "nat_book_chap": 1, 
     "dept_code": "109", 
     "dept_name": "AS" 
    }, 
    "7": { 
     "int_jour": 0, 
     "nat_jour": 0, 
     "inter_nat_conf": 0, 
     "nat_conf": 0, 
     "int_book_chap": 0, 
     "nat_book_chap": 0, 
     "dept_code": "110", 
     "dept_name": "CIVIL" 
    } 
} 
+2

yorコードを関連する部分に減らし、何が機能していないかを指定してください。 –

+0

ブラウザで 'F12'を使用してdevのconosleを開き、ajax経由で呼び出されたものと実際に返されたもののネットワークタブを確認します。 – JustOnUnderMillions

答えて

0

PHPのson_encodeが配列を返してから配列インデックス(インデックスだけでなく)が必要

document.getElementById("demo").innerHTML = myObj.[0].dept_name; 
関連する問題