2016-06-22 6 views
0

私の問題についてこのサイトの多くのトピックを検索しました。彼らの多くは私を助けましたが、私はまだ解決できない大きな問題があります。jsの変数としてのphpの結果

私はSQLデータベースからphpファイルを介してデータを取得します。このphpファイルは、データをjson-stringに変換します。今私はこのデータから双曲線ツリーを構築するためにこのjson Stringをjs-Fileの中に入れたいと思っています。

Iは、PHPを有する:

問題は、この程度であり、この出力を有する

<?php 
 

 
$categories = Category::getTopCategories(); 
 
$categories \t = array_values($categories); 
 

 
echo json_encode($categories) 
 

 
class Category 
 
{ 
 
/** 
 
* The information stored in the database for each category 
 
*/ 
 
public $id; 
 
public $parent; 
 
public $name; 
 

 
// The child categories 
 
public $children; 
 

 
public function __construct() 
 
{ 
 
    // Get the child categories when we get this category 
 
    $this->getChildCategories(); 
 
} 
 

 
/** 
 
* Get the child categories 
 
* @return array 
 
*/ 
 
public function getChildCategories() 
 
{ 
 
    if ($this->children) { 
 
     return $this->children; 
 
    } 
 
    return $this->children = self::getCategories("parent = {$this->id}"); 
 
} 
 

 
//////////////////////////////////////////////////////////////////////////// 
 

 
/** 
 
* The top-level categories (i.e. no parent) 
 
* @return array 
 
*/ 
 
public static function getTopCategories() 
 
{ 
 
    return self::getCategories('parent = 0'); 
 
} 
 

 
/** 
 
* Get categories from the database. 
 
* @param string $where Conditions for the returned rows to meet 
 
* @return array 
 
*/ 
 
public static function getCategories($where = '') 
 
{ 
 
    if ($where) $where = " WHERE $where"; 
 
\t $conn=mysqli_connect('localhost', 'root', '','praktikum'); 
 
\t $sql = "SELECT * FROM nugget$where"; 
 
    $result = mysqli_query($conn,$sql); 
 

 
    $categories = array(); 
 
    while ($category = mysqli_fetch_object($result, 'Category')) 
 
     $categories[] = $category; 
 

 
    mysqli_free_result($result); 
 
    return $categories; 
 
} 
 
} 
 

 
?>

[{ 
"id": "1" 
, "parent": "0" 
, "name": "WI2" 
, "children": [{ 
    "id": "2" 
    , "parent": "1" 
    , "name": "E-Business" 
    , "children": [{ 
     "id": "4" 
     , "parent": "2" 
     , "name": "Vorlesung" 
     , "children": [] 
    }, { 
     "id": "5" 
     , "parent": "2" 
     , "name": "Uebung" 
     , "children": [] 
    }] 
}, { 
    "id": "3" 
    , "parent": "1" 
    , "name": "E-Procurement" 
    , "children": [{ 
     "id": "6" 
     , "parent": "3" 
     , "name": "Vorlesung" 
     , "children": [] 
    }, { 
     "id": "7" 
     , "parent": "3" 
     , "name": "Uebung" 
     , "children": [] 
    }] 
}] 

}]

と私の代わりに別々のファイル(example.js)であり、このjavascriptの方法で「JSON」の背後にあるテキストの出力を持つようにしたい:

function init(){ 
//init data 
var json = { /* 
Hier stehen die ganzen Kinder drinnen 
Einzelne Children durch Datenbankeinträge ersetzen 
*/ 

    **"id": "1" 
    , "parent": "0" 
    , "name": "WI2" 
    , "children": [{ 
     "id": "2" 
     , "parent": "1" 
     , "name": "E-Business" 
     , "children": [{ 
      "id": "4" 
      , "parent": "2" 
      , "name": "Vorlesung" 
      , "children": [] 
     }, { 
      "id": "5" 
      , "parent": "2" 
      , "name": "Uebung" 
      , "children": [] 
     }] 
    }, { 
     "id": "3" 
     , "parent": "1" 
     , "name": "E-Procurement" 
     , "children": [{ 
      "id": "6" 
      , "parent": "3" 
      , "name": "Vorlesung" 
      , "children": [] 
     }, { 
      "id": "7" 
      , "parent": "3" 
      , "name": "Uebung" 
      , "children": [] 
     }] 
    }]** 


}; 

ので、どのように私は変数$カテゴリを得るのですかjavascriptファイルのようにjsonは私が手続きをするときに得られる出力と同じ出力を内部に持っています: 'echo json_encode($ categories); [{開始時と終了時にタグ}を付けずに

$ .get( "Testerich.php");で試してみました。他のfuntionsしかしそれは仕事をしなかった。あなたは、あなたがしようとした言ったようにAJAXリクエストを行う必要がありますので、あなたは.jsファイル内のPHP変数を使用することはできませんが、一例では、あなたが使用していない提供

+0

PHPを使用してjsコードを入力していますか?そして、あなたが必要とするのは '[{'と '}]を文字列から取り除くことです? – k102

+0

JSON.parse( "json text")を使用します。 –

答えて

0

は、あなたのPHPはこのようにようにJSONエンコードされたデータを返すことを仮定します。コールバックは、必ずので(自分のコードに示すように、あなたはjQueryのを使用していると仮定した場合)のような要求を行っている作ります/ PHP

<?php 
function getCategory() 
{ 
     echo json_encode(getCat()); 
} 
.... 
?> 

// JavaScriptのコード

var myCategory ; 

$.ajax({ 

    url : "your-domain.com/...../getCategory", 
    method : "POST", 
    success : function (response) 
    { 
      myCategory = JSON.parse(response); 

      // Now you have category information in JSON format 
      // Your code here 
    } 

});

JavaScriptでmyCategory変数を使用してJSONツリーにアクセスできます。

JavaScriptを使用中

console.log(myCategory); 

変数の構造を知ります。

0

アラジンあなたの助け ため

感謝

/- :

$.get("Testerich.php", function(data) { 
    console.log(JSON.parse(data)); // use JSON.parse() to convert the string returned by "Testerich.php" to JSON 
}); 
関連する問題