MySQL, PHP, JsTree, Example

979 views
Skip to first unread message

MGxpwr

unread,
May 30, 2014, 5:28:54 AM5/30/14
to jst...@googlegroups.com
It does seem i can't get the combo work. What im trying to do is:

I try to get data form my MySql database put it in a json_encode and put that data in my jsTree.
I loads my php file for the database handling but then i don't see the data in my jsTree

Index.html:
<html>
    <head>
        <link rel="stylesheet" href="dist/themes/default/style.min.css" />
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script src="dist/jstree.min.js"></script>
        <script>
$(function() {
$.ajax({
async : true,
type : "GET",
url : "server.php",
dataType : "json",    

success : function(json) {
createJSTrees(json);
},    

error : function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});    

function createJSTrees(jsonData) {
$("#jstree").jstree({
"json_data" : {
"data" : jsonData
},
"plugins" : [ "themes", "json_data", "ui" ]
});
}
        </script>
    </head>
    <body>
        <div id="jstree">
        
        </div>
    </body>
</html>

server.php:

<?php
   //up here is have the db connect stuff but i took it away for save keeping!! just pretend its here  
    
    $sql = "";
    
    $q =  "SELECT * FROM mytree WHERE parent_id = '0'";
    
    $q = mysql_query($q);
    $rows = array();
    while($r = mysql_fetch_assoc($q)) {
        $rows[] = $r;
    }
   echo json_encode($rows);
            
?>


I hope anyone can help me with this.

Once i get this to work i want to enabel the dnd plugin and start draggin stuff around and he has the save the new position to the database! But thats for later

Thanks in advance,
Peter

Ivan Bozhanov

unread,
May 30, 2014, 3:03:15 PM5/30/14
to jst...@googlegroups.com
Sorry, but you have not provided enough information so that I can help you. My guess is you are not returning the structure required by jstree.
What does "json" contain (createJSTrees(json))? Log it out and paste it here.

Best regards,
Ivan
Reply all
Reply to author
Forward
0 new messages