【发布时间】:2018-07-15 03:33:38
【问题描述】:
我是 jQuery 和 PHP 的新手。
我正在开发一个网页来显示一组从数据库中获取的记录。在这里,我使用了 PHP 和 jQuery。
我需要将一组记录显示为表格。使用 php 从 MySQL 数据库中检索数据。使用 json_encode() 将一组行作为字符串传递给 html 页面。
问题是我无法在表格中逐行显示这些数据。我正在使用使用<div> 创建的表。所以我需要知道如何逐行显示这串数据并分离每一列的值。
这是我所做的只显示一行,但数据没有显示为表格。也没有编译错误。我也需要帮助来扩展它以显示多行。
demo.html(我要显示记录的页面):
<div class="table">
<div class="headRow">
<div class="cell">ID</div>
<div class="cell">First Name</div>
<div class="cell">Last Name</div>
<div class="cell">Age</div>
<div class="cell">Class</div>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'beta.php' ,
data:"",
dataType: 'json',
success:function(data){
var elementArray = new Array(); //creating the array
elementArray = data.split(""); //splitting the string which was passed using json_encode()
var id = elementArray[0]; //passing values corresponding to the columns
var fname = elementArray[1];
var lname = elementArray[2];
var age = elementArray[3];
var grade = elementArray[4];
$("<div>", { //creating a new div element and assiging the value and appending it to the column 1
"class":"cell",
"text":id
})
.appendTo("document.body");
$("<div>", { //cloumn 2 value
"class":"cell",
"text":fnam
})
.appendTo("document.body");
$("<div>", { //cloumn 3 value
"class":"cell",
"text":lname
})
.appendTo("document.body");
$("<div>", { //cloumn 4 value
"class":"cell",
"text":age
})
.appendTo("document.body");
$("<div>", { //cloumn 5 value
"class":"cell",
"text":grade
})
.appendTo("document.body");
}
});
});
</script>
</div>
</div>
demo.php(从数据库中检索数据):
$result = mysql_query("SELECT * FROM student WHERE StuId=1",$con) or die (mysql_error());
$resultArray = mysql_fetch_row($result);
echo json_encode($value);
如果有人可以帮助我,那就太好了。
【问题讨论】:
-
你想用 div 或 tr td 来创建表吗?
标签: php jquery mysql ajax css-tables