gpt4 book ai didi

php - 使用 MySQL 数据填充 div,将行与编号 ID 匹配

转载 作者:行者123 更新时间:2023-12-03 18:47:44 26 4
gpt4 key购买 nike

我正在尝试使用书籍数据库通过将可点击 div 的 id 与数据库中每本书的序列号进行匹配来在页面上显示相关数据。我的表是这样的:

+-------------------+----------------+------------------+------------------+
| booknumber (INT) | title (VCHAR) | author (VCHAR) | publisher (VCHAR)|
+-------------------+----------------+------------------+------------------+
| 123 | title of book | name | publisher name |
| 124 | title of book | name | publisher name |
| 125 | title of book | name | publisher name |
| 127 | title of book | name | publisher name |
| 128 | title of book | name | publisher name |
| 130 | title of book | name | publisher name |
--------------------------------------------------------------------------
多亏了 this thread 中的答案,我才能获得要显示的数据.
HTML
    <html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="book_db.js"></script>
</head>
<body>

<div class="index-wrapper">
<div class="book" id="123">Book title</div>
<div class="book" id="124">Book title</div>
<div class="book" id="125">Book title</div>
<div class="book" id="127">Book title</div>
<div class="book" id="128">Book title</div>
<div class="book" id="130">Book title</div>
</div>

<div class="book-info">
<h2 id='title'></h2>
<span id='booknumber'></span>
<span id='author'></span>
<span id='pubhisher'></span>
</div>

</body>
</html>
php
<?php
$hostname = "localhost";
$username = "***";
$password = "***";
$databaseName = "book_db";


$connect = mysqli_connect($hostname, $username, $password, $databaseName);

$query = "SELECT booknumber, title, author, publisher FROM booknumber";

$result = mysqli_query($connect, $query);

while($row = mysqli_fetch_array($result))
{
$dataRow[] = $row;
}

echo json_encode($dataRow);
?>
和jQuery
$(document).ready(function() {
$.post("book_db.php", {

}, function(server_data) {
data = JSON.parse(server_data);
$(document).on('click', '.book', function(){
var number = $(this).attr('id');

$("#title").html(data[number]['title']);
$("#booknumber").html(data[number]['booknumber']);
$("#author").html(data[number]['author']);
$("#publisher").html(data[number]['publisher']);

});
});
});
这对前 125 本书很有用,但是有一个部分,实际书上的数字跳过了一个数字,因为这些书没有被使用。那时我意识到这段代码是从行号而不是我的 INT 数据(书的实际编号)中提取的。
所以我要完成的就是用数据库中书号(INT)对应的行的数据(不是行号)填充info div里面的文本。如果这对解决方案有任何影响,我将 booknumber 列设置为我的主键。

最佳答案

通过获取完整的表数据实际上是在浪费服务器资源,而实际上您需要显示一本书数据。在点击甚至触发后发出您的帖子请求。将“数字”数据传送到服务器,从表中获取对应的行数据,然后显示。

$(document).ready(function() {
$(document).on('click', '.book', function(){
var number = $(this).attr('id');

$.post("book_db.php", {result: number}, function(server_data) {
data = JSON.parse(server_data);

$("#title").html(data[number]['title']);
$("#booknumber").html(data[number]['booknumber']);
$("#author").html(data[number]['author']);
$("#publisher").html(data[number]['publisher']);

});
});
});

您必须根据此重写服务器代码。
另外,尝试动态创建这些 html 元素

<div class="index-wrapper">
<div class="book" id="123">Book title</div>
<div class="book" id="124">Book title</div>
<div class="book" id="125">Book title</div>
<div class="book" id="127">Book title</div>
<div class="book" id="128">Book title</div>
<div class="book" id="130">Book title</div>
</div>

PHP

?php
$hostname = "localhost";
$username = "***";
$password = "***";
$databaseName = "book_db";


$connect = mysqli_connect($hostname, $username, $password, $databaseName);

$dataRow = array();
if(isset($_POST['result'])) {
$post = $_POST['result'];
$sql = "SELECT * FROM booknumber WHERE booknumber=".$post;
$result = $connect->query($sql);


while($row = $result->fetch_assoc())
{
$dataRow[] = $row;
}
}

echo json_encode($dataRow);
?>

关于php - 使用 MySQL 数据填充 div,将行与编号 ID 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67630417/

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