提问人:5admin 提问时间:6/9/2022 最后编辑:5admin 更新时间:6/9/2022 访问量:56
从手风琴获取数据和内部数据
Get data and inner data from accordion
问:
我正在尝试制作一个从两个不同表加载用户数据的手风琴,但我似乎无法让它工作。
为了理解我的意思,我有一个片段,我想实现什么:
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
.active, .accordion:hover {
background-color: #ccc;
}
.panel {
padding: 0 18px;
display: none;
background-color: white;
overflow: hidden;
}
<h2>Accordion loading user data</h2>
<button class="accordion">John Doe</button>
<div class="panel">
<p>FTP Host: domain.com</p>
<p>FTP Username: johndoe</p>
<p>FTP Password: test123</p>
<hr>
<p>FTP Host: second-domain.com</p>
<p>FTP Username: johndoe</p>
<p>FTP Password: test123</p>
</div>
<button class="accordion">Jack Pepperidge</button>
<div class="panel">
<p>FTP Host: domain.com</p>
<p>FTP Username: jacky</p>
<p>FTP Password: test123</p>
<hr>
<p>FTP Host: second-domain.com</p>
<p>FTP Username: jacky</p>
<p>FTP Password: test123</p>
</div>
<button class="accordion">Sam Parker</button>
<div class="panel">
<p>FTP Host: domain.com</p>
<p>FTP Username: sammy</p>
<p>FTP Password: test123</p>
<hr>
<p>FTP Host: second-domain.com</p>
<p>FTP Username: sammy</p>
<p>FTP Password: test123</p>
</div>
我有两个名为 customers 和 customers_details 的表
客户 | customers_details |
---|---|
customer_id | ftp_host |
customer_name | ftp_username |
customer_company | ftp_password |
customer_id |
现在我运行的问题是我有这个脚本:
<?php
$stmt = $con->prepare("SELECT customers.customer_id, customers.customer_name, customers.customer_company, customers_details.ftp_host, customers_details.ftp_username, customers_details.ftp_password
FROM customers
LEFT JOIN customers_details ON customers.customer_id = customers_details.customer_id WHERE customers.customer_id > ?
ORDER BY customers.customer_company ASC");
$biggerThan = 0;
$stmt->bind_param("i", $biggerThan);
$stmt->execute();
$result = $stmt->get_result();
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$customer_company = $row['customer_company'];
$customer_name = $row['customer_name'];
$customer_id = $row['customer_id'];
$rows[] = $row;
?>
<button class="accordion">John Doe</button>
<div class="panel">
<?php
foreach ($rows as $row) {
// FTP details
$ftp_host = htmlentities($row['ftp_host']);
$ftp_username = htmlentities($row['ftp_username']);
$ftp_password = htmlentities($row['ftp_password']);
?>
<div class="panel">
<p>FTP Host: <?= $ftp_host; ?></p>
<p>FTP Username: <?= $ftp_username; ?></p>
<p>FTP Password: <?= $ftp_password; ?></p>
<hr>
<p>FTP Host: <?= $ftp_host ?></p>
<p>FTP Username: <?= $ftp_username; ?></p>
<p>FTP Password: <?= $ftp_password; ?></p>
</div>
<?php
}
}
} else {
echo '<div class="alert alert-danger" role="alert">No user details found</div>';
}
?>
但是当我运行它时,我得到
约翰·多伊 约翰·多伊
杰克·佩珀里奇
杰克·佩珀里奇 山姆·帕克
山姆·帕克
我尝试使用 UNION 更改语句,但似乎没有任何效果。
答:
0赞
bloodyKnuckles
6/9/2022
#1
首先查询数据库,用于确保每个客户只获得一行。然后循环使用结果,并为每个客户写下您的 and:SELECT DISTINCT
<button class="accordion">
<div class="panel">
<?php
// added SELECT DISTINCT to ensure a single row per customer
$stmt = $con->prepare("SELECT DISTINCT customers.customer_id, customers.customer_name, customers.customer_company, customers_details.ftp_host, customers_details.ftp_username, customers_details.ftp_password
FROM customers LEFT JOIN customers_details ON customers.customer_id = customers_details.customer_id WHERE customers.customer_id > ?
ORDER BY customers.customer_company ASC");
$biggerThan = 0;
$stmt->bind_param("i", $biggerThan);
$stmt->execute();
$result = $stmt->get_result();
$num_rows = mysqli_num_rows($result);
if ($num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$customer_company = $row['customer_company'];
$customer_name = $row['customer_name'];
$customer_id = $row['customer_id'];
// removed $rows[] = $row;
?>
<button class="accordion"><?= $customer_company; ?></button>
<div class="panel">
<?php
// removed foreach ($rows as $row) {
// $row already contains the fields:
// customer_id, customer_name, customer_company,
// ftp_host, ftp_username, ftp_password
// FTP details
$ftp_host = htmlentities($row['ftp_host']);
$ftp_username = htmlentities($row['ftp_username']);
$ftp_password = htmlentities($row['ftp_password']);
?>
<div class="panel">
<p>FTP Host: <?= $ftp_host; ?></p>
<p>FTP Username: <?= $ftp_username; ?></p>
<p>FTP Password: <?= $ftp_password; ?></p>
<hr>
<p>FTP Host: <?= $ftp_host ?></p>
<p>FTP Username: <?= $ftp_username; ?></p>
<p>FTP Password: <?= $ftp_password; ?></p>
</div>
<?php
} // END WHILE $row
} // END IF $num_rows
} else {
echo '<div class="alert alert-danger" role="alert">No user details found</div>';
}
?>
评论
}
while
foreach
customers_details
$rows[] = $row;
foreach ($rows as $row) {
while
$rows