从手风琴获取数据和内部数据

Get data and inner data from accordion

提问人:5admin 提问时间:6/9/2022 最后编辑:5admin 更新时间:6/9/2022 访问量:56

问:

我正在尝试制作一个从两个不同表加载用户数据的手风琴,但我似乎无法让它工作。

为了理解我的意思,我有一个片段,我想实现什么:

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 更改语句,但似乎没有任何效果。

php mysql while-loop foreach

评论

0赞 bloodyKnuckles 6/9/2022
您的示例代码缺少一个 curly: 。要么 .}whileforeach
0赞 bloodyKnuckles 6/9/2022
每个客户在表中有多少行?...是否确定?customers_details
0赞 bloodyKnuckles 6/9/2022
这个:然后是这个:对我来说没有意义。显然,在一个循环中,您将一个元素附加到数组中,然后在该数组上循环......?我错过了什么?$rows[] = $row;foreach ($rows as $row) {while$rows
0赞 CBroe 6/9/2022
请将您显示的代码归结为下次所需的最低限度。我们知道手风琴是什么,你向我们展示的所有与此相关的JS和CSS,似乎都与你所问的实际问题无关。

答:

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>';
}

?>