php - 如何从 MySQL 中的查询中使用其各自的数据创建手风琴?
问题描述
我已经准备好了一切,我正在提取章节和视频系列,一切都没有问题:
$id_course = 1;
$stmt = $con->prepare("SELECT c.chapter,
v.preview,
v.title_video,
v.description_video,
v.type_format,
v.multimedia,
v.detail,
v.time_video,
v.url_website
FROM tbl_chapters c
JOIN tbl_videos v ON v.id_course = c.id_course AND v.id_chapter = c.id_chapter
WHERE c.id_course=?
");
$stmt->bind_param("i", $id_course);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows>0) {
$stmt->bind_result($chapter, $preview, $title_video, $description_video, $type_format, $multimedia, $detail, $time_video, $url_website);
while ($stmt->fetch()) {
}
} else {
echo "no data!";
}
每章$chapter
都有其系列视频,可以通过以下格式看到:
<div class="modules">
<button class="accordion">Introduction</button>
<div id="enlaces" class="section videolist" style="display: none;">
<a class="link playing" href="#">
<div class="chapter flex">
<div><span class="check-mark"></span></div>
<div><span class="play-title-course">Video 1</span></div>
<div><span class="time-video">00:41</span></div>
</div>
</a>
<a class="link playing" href="#">
<div class="chapter flex">
<div><span class="check-mark"></span></div>
<div><span class="play-title-course">Video 2</span></div>
<div><span class="time-video">00:41</span></div>
</div>
</a>
</div>
</div>
<div class="modules">
<button class="accordion">Introduction II</button>
<div id="enlaces" class="section videolist" style="display: none;">
<a class="link playing" href="#">
<div class="chapter flex">
<div><span class="check-mark"></span></div>
<div><span class="play-title-course">Class 1</span></div>
<div><span class="time-video">00:41</span></div>
</div>
</a>
<a class="link playing" href="#">
<div class="chapter flex">
<div><span class="check-mark"></span></div>
<div><span class="play-title-course">Class 2</span></div>
<div><span class="time-video">00:41</span></div>
</div>
</a>
</div>
</div>
问题是,如果我从$chapter
循环中打印出来,它会为每个存在的视频系列重复给我,而且不应该是这样,我附上了我想要实现的屏幕截图(注意:html 格式已经有效如图):
这是数据表:
你能指导我吗
解决方案
要停止 $chapter 的重复性质,请使用 if 语句和临时变量。使“循环”成为这个。
$tempChapter = "";
while ($stmt->fetch()) {
// If you have not yet printed the $chapter, this will be true
if ($tempChapter != $chapter) {
echo $chapter;
// This stores the current chapter in $tempChapter, so this code will not run again until $chapter changes
$tempChapter = $chapter;
}
echo $title_video;
}
您应该能够添加到此以制作您想要的 HTML 结构。
推荐阅读
- javascript - 如何使我的电子应用程序表现得像移动设备
- sql - 如何查询 datetime 列以查找 case 语句中可能出现乱序的日期?
- c - 刚接触 Visual Studio Code 和 C,遇到困难
- javascript - 如何使用钩子使用数据对象更新 React 状态
- macos - minTimeout 大于 maxTimeout
- c - 如何在 C 中转储未知的内存结构
- python-3.x - 如何修复“ImportError:没有名为‘x’的模块”
- android - Flutter:系统 UI 'Location Permissions' 对话框破坏了代码执行的连续性?
- python - 如何使用 Python 将我的 DataFrame 转换为雷达图?
- javascript - Hapi Joi - 验证连接键