我正在开发一个简单的网络应用程序,它读取与特定 ID 匹配的名称作为输入,并在 HTML 表格中显示名称。稍后,我可能会考虑分页(显示更多按钮等)。
我的 PHP 错误日志或控制台中没有收到任何错误,但是当我打开页面时没有任何显示。
我的数据库名为 DataList,包含名为 Name
和 ID
的列。
PHP(后端运行):
$sql = "SELECT Name FROM DataList WHERE ID=1";
$result = $conn->query($sql);
$htmlTable = '<table border="1" cellspacing="0" cellpadding="2">
<tr>';
$haveTable = 'no';
if ($result->num_rows > 0) {
$haveTable = 'yes';
while($row = $result->fetch_assoc()) {
$htmlTable .= "<tr><td>".$row["Name"]."</td></td>";
}
$htmlTable .= "</tr>";
return "$htmlTable";
} else {
echo "You haven't input anything yet!";
}
mysqli_close($db);
?>
我的 HTML 代码段:(文件名为 data.html,位于 View 文件夹和正文的 div 中)
<form id="nameViewForm" >
<script> window.onload="displayNames()" ; </script>
<div id="nameTable"></div>
<button type="button" ng-click="switchForm('#dataInputForm')">Input
Data</button>
</form>
我的 JS Controller (名为 user.js 的文件,位于 Controller 文件夹中。)
app.controller('dataCtrl', function($scope, $http) {
$http.get("<website>/displayNames.php").then(function(res){
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
$scope.displayNames = function() {
var data = $.param({
});
$http.get("<website>/displayNames.php", data, config).then(function(res) {
console.log();
var table = data.property;
$scope.nameTable = table;
});
}
});
我的逻辑是否存在根本性缺陷,或者我在其中一个部分搞砸了?
最佳答案
这就是我通常在表格中显示数据的方式。
首先,我在 html 文件中创建一个表格,如下所示:
<table>
<tbody>
<tr ng-repeat="n in names">
<td>{{n.name}}</td>
</tr>
</tbody>
</table>
并使用 ng-repeat 显示数据。
在我的 js 中,我获取所有名称并将它们放入范围变量中。
var app = angular.module('app', []);
app.controller('dataCtrl', function ($scope, $http) {
$scope.getnames = function(){
$http.get('<website>/displayNames.php')
.then(function(response) {
$scope.names = response.data;
});
}
$scope.getnames();
});
在我的 php 文件中,我像这样简单地回显 json 数据
$sql = "SELECT Name FROM DataList WHERE ID=1";
$result = $conn->query($sql);
$names = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$names[] = $row;
}
}
$JSONReady = json_encode($names);
echo $JSONReady;
关于javascript - 如何在 MVC(AngularJS、HTML、PHP)中显示来自 SQL 数据库的特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49824397/