首页 > 解决方案 > 如何从 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 格式已经有效如图):

在此处输入图像描述

这是数据表:

在此处输入图像描述

你能指导我吗

标签: phphtml

解决方案


要停止 $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 结构。


推荐阅读