1
我有一個產品表,其中插入了所有新產品。此產品表包含產品名稱product_name
和產品所屬部門department
。每次循環引導標籤並創建按鈕
我正在嘗試遍歷我的產品,以生成包含部門標題和所有與每個部門關聯的產品(作爲按鈕)作爲標籤內容的選項卡。
到目前爲止,我只設法顯示部門作爲標籤標題和與每個部門相關的第一批產品。
任何幫助或推動正確的方向將非常讚賞。
$i = 0;
$j = 0;
$deptData = array();
$query = "SELECT `product_name`, `department`
FROM `products`
WHERE `status` = 'Active'
GROUP BY CONCAT(`department`, `product_name`)";
$stmt = $mysqli->prepare($query);
$stmt->execute();
$stmt->bind_result($product_name, $department);
$stmt->store_result();
while($stmt->fetch()) {
$deptData[$department][$product_name]['product_name'] = $product_name;
}
$stmt->close();
<div class="container">
<div class="row">
<div class="col-sm-12">
<ul class="nav nav-tabs">
<?php
$tabTitle = "";
$tabTitleClass = "";
foreach($deptData as $department => $product_name) {
if($i == 0) {
$tabTitleClass = "active";
} else {
$tabTitleClass = "";
}
/* Build our tab pane title */
$tabTitle = '<li class="'.$tabTitleClass.'"><a href="#'.$department.'" data-toggle="tab">'.$department.'</a></li>';
echo $tabTitle;
$i++;
} ?>
</ul>
<div class="tab-content">
<?php
$tabContent = "";
$tabContentClass = "";
foreach($deptData as $department => $product_in_dept) {
foreach($product_in_dept as $product => $value) {
if($j == 0) {
$tabContentClass = "tab-pane fade in active";
} else {
$tabContentClass = "tab-pane fade";
}
/* Build our tab content */
$tabContent = ' <div id="'.$department.'" class="'.$tabContentClass.'">
<button class="btn btn-default">'.$value['product_name'].'</button>
</div>';
echo $tabContent;
$j++;
}
}
?>
</div>
</div>
</div>
</div>