2015-11-03 55 views
2

我對PHP很新,我正嘗試使用Datatables jQuery插件。我知道我必須從.php文件中獲取數據,然後在我的數據表中使用它,但似乎無法通過ajax將它傳遞給數據表。我設法從數據庫中獲取我的數據,然後json對它進行編碼,但是我不知道如何將它們調用到我的index.php文件中,在那裏我需要顯示它。我究竟做錯了什麼? 這裏是我的代碼:如何從php獲取json數據用於數據表jquery插件

HTML(編輯):

 <!-- DataTables CSS --> 
< link rel='stylesheet' type='text/css' href='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css'> 

< !-- jQuery --> 
< script type='text/javascript' charset='utf8' src='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js'></script> 

<!-- DataTables --> 
< script type='text/javascript' charset='utf8' src='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js'></script> 

< script type='text/javascript'> 
    $(document).ready(function() { 
$('#tabela').dataTable({ 
        'bJQueryUI' : true, 
        'sPaginationType' : 'full_numbers', 
        'bRetrieve' : true, 
        'bFilter' : true, 
        'iDisplayLength': 10, 
        'bProcessing' : true, 
        "sAjaxSource": "adminmysqli.php" 

     }); 
}); 
< /script> 

從fetchdata.php數據:

{"iTotalRecords":"41","iTotalDisplayRecords":"41","aaData": 
[["2","Arya","Stark","612345555","Braavos"], 
["3","Jon","Snow","612345655","The"], 
["4","Meryn","Trant","612345679","Tirane"], 
["5","Sansa","Stone","692345678","Durres"], 
["6","Myda","Slate","612345676","Tirane"], 
["7","Varys","Bird","612345689","Tirane"], 
["9","Stannis","Baratheon","612345678","Tirane"], 
["10","Shireen","Burn","612345678","Iron"], 
["11","Selyse","Tully","612345678","Tirane"], 
["12","Lyanna","Dread","612345678","Tirane"], 
["13","Viserys","Targaryen","612345678","Durres"], 
["14","Daennerys","Stormborn","612345678","Tirane"], 
["15","Khaal","Drogo","612345678","Tirane"], 
["16","Jojen","Reed","612345678","Tirane"], 
["17","Theon","Greyjoy","612345678","Tirane"], 
["18","Osha","Green","612345678","Tirane"], 
["19","Oberyn","Martell","612345678","Tirane"], 
["20","Ellaria","Sands","612345678","Tirane"], 
["22","Tommen","Laster","612345678","Tirane"], 
["23","Robert","Baratheon","612345678","Tirane"], 
["24","Jamie","Lannister","612345678","Tirane"], 
["25","Tywin","Bolt","612345678","Tirane"], 
["26","Tyrion","Imp","612345678","Tirane"], 
["27","Gregor","Clegane","612345678","Tirane"], 
["28","Qyburn","Exper","612345678","Tirane"], 
["30","Daniel","Howell","612345678","Tirane"], 
["31","Samwell","Tarly","612345678","Tirane"], 
["32","Aemon","Maester","61234569","Tirane"], 
["33","Jaqen","Hgar","612345678","Tirane"], 
["34","Daario","Naharis","612345678","Tirane"], 
["35","Jorah","Bear","612345678","Tirane"], 
["36","Irri","Mereen","612345678","Tirane"], 
["37","Margaery","Tyrell","612345678","Tirane"], 
["38","Renly","Baratheon","612345678","Tirane"], 
["44","Eddard","Stark","697845123","Winterfell"], 
["45","Miranda","Hart","692314256","Durres"], 
["46","Stray","Cat","691234567","Here"], 
["48","Haley","Dunphy","653746111","Tirana"], 
["51","Asli","Felin","666111222","South"], 
["55","Alison","Brie","667755333","Saye"], 
["58","Marie","Curie","665544333","Radium"]]} 

編輯: 謝謝您的回覆,我設法讓格式化數據我刪除了mData屬性,我也錯誤地加載了一個腳本,阻止我獲取數據。

回答

1

更改sAjaxSource路徑到你的PHP文件,並再次嘗試使用這些代碼:

<script type='text/javascript'> 
$(document).ready(function() { 
    $('#tabela').dataTable({ 
     'bJQueryUI' : true, 
     'sPaginationType' : 'full_numbers', 
     'bRetrieve' : true, 
     'bFilter' : true, 
     'iDisplayLength': 10, 
     'bProcessing' : true, 
     "sAjaxSource": "fetchdata.php" 
    }); 
}); 
</script>