gpt4 book ai didi

php - 基于 mySQL 数据动态创建 HTML 元素

转载 作者:行者123 更新时间:2023-11-29 06:00:26 26 4
gpt4 key购买 nike

您好,目前我有一组代码可以使用外部 JSON 数据通过 AJAX 动态创建 HTML 元素。当我创建它时,我想将我的数据存储在外部文件而不是数据库中。

但是,我现在需要将它们存储在 mySQL 中,所以我想知道如何仍然可以像这样动态创建 HTML 元素,但现在数据将来自 mySQL,而不是从外部 JSON 文件中检索。我对此还是很陌生,所以我真的很困惑如何解决这种情况。

这是我当前代码的样子:

 <script>
$.ajax({
url : "CR/CR_Data/CR_QuickLookData.json",
type : "post",
contentType:"application/json",
success : function(list){
var divCol = "<div class='col-sm-4 col-md-4'>";
var divWell = "<div class='well' style='position:relative'>";
var divClose = "</div>";

console.log(list);

list.forEach(function(obj, index) {

//console.log(obj);

var title = "<h5>" + obj.title + "</h5>";
var linkStart = "<a href='" + obj.imagePath + "' rel='lightbox' title='" + obj.title + "'>"
var image = "<img data-toggle='tooltip' data-placement='left' class='wellImg' title='Click to enlarge image' src='" + obj.imagePath + "'/>"
var desc = "<p>" + obj.desc + "</p>";
var linkEnd = "</a>";

var div = divCol +
divWell +
title +
linkStart +
image +
desc +
linkEnd +
divClose +
divClose;

$("#CR").append(div); // insert the div you've just created

})
}
});
</script>

JSON 数据:

 [  
{
"team":"Team Name",
"title":"Title",
"filePath":"#",
"imagePath":"image path",
"desc":"Data Description"
},
{
"team":"Team Name",
"title":"Title",
"filePath":"#",
"imagePath":"image path",
"desc":"Data Description"
},
{
"team":"Team Name",
"title":"Title",
"filePath":"#",
"imagePath":"image path",
"desc":"Data Description"
}
]

当我尝试使用 PHP 提取数据并将其编码为 JSON 时,它给了我这个结果,但它没有创建任何所需的 HTML 元素。

enter image description here

最佳答案

你可以像这样创建一个 MySQL 表,

CREATE TABLE `record` (
`id` INT NOT NULL AUTO_INCREMENT,
`team` TEXT NULL,
`title` TEXT NULL,
`filePath` TEXT NULL,
`imagePath` TEXT NULL,
`desc` TEXT NULL,
PRIMARY KEY (`id`));

然后你可以使用这个查询来插入你的数据,

INSERT INTO `record` (`team`, `title`, `filePath`, `imagePath`, `desc`) VALUES ('Team Name', 'Title', '#', 'image path', 'Data Description');

之后,您可以创建一个 PHP 文件来提取数据并以 JSON 格式打印,

您可以为 PHP 文件指定一个文件名,例如,

CR_QuickLookData.php

用PHP代码,

<?php

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT team,title,filePath,imagePath,`desc` FROM record";
$result=mysqli_query($con,$sql);

$list = array();
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
$list[] = array (
'team' => $row['team'],
'title' => $row['title'],
'filePath' => $row['filePath'],
'imagePath' => $row['imagePath'],
'desc' => $row['desc']
);
}

mysqli_free_result($result);
mysqli_close($con);


echo json_encode($list);

然后在index.html中,

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="CR"></div>
<script>
$(window).ready(function(e) {
$.ajax({
url : "CR_QuickLookData.php",
type : "post",
dataType: "json",
success : function(list){
var divCol = "<div class='col-sm-4 col-md-4'>";
var divWell = "<div class='well' style='position:relative'>";
var divClose = "</div>";

list.forEach(function(obj, index) {
var title = "<h5>" + obj.title + "</h5>";
var linkStart = "<a href='" + obj.imagePath + "' rel='lightbox' title='" + obj.title + "'>"
var image = "<img data-toggle='tooltip' data-placement='left' class='wellImg' title='Click to enlarge image' src='" + obj.imagePath + "'/>"
var desc = "<p>" + obj.desc + "</p>";
var linkEnd = "</a>";

var div = divCol +
divWell +
title +
linkStart +
image +
desc +
linkEnd +
divClose +
divClose;

$("#CR").append(div); // insert the div you've just created
});
}
});
});
</script>
</body>
</html>

关于php - 基于 mySQL 数据动态创建 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45474770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com