检查in_array值

时间:2018-08-11 06:41:43

标签: php mysql

Image for explaining my question.

上面的图像是我想要实现的,唯一的问题是回显主题和主题,而主题位于主题之下,但是我只想回声说英语,英语下的所有主题都将在英语之下。但是我所拥有的是每个主题都有其自己的主题。因此,从图像两个英语下拉列表中。 这是代码。    

<?php
    $sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2'";
    $result = $pdo->query($sql);
    $result->setFetchMode(PDO::FETCH_ASSOC);
    while($row=$result->fetch()){
        if(!in_array('subject', $row['subject']){
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<ul class="list-unstyled">
    <li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
    </li>
</ul>
</div>
</li>
<?php
        } else {
            echo ""
        }
    }

?>

1 个答案:

答案 0 :(得分:0)

这应该输出主题和主题,并允许每个主题多个主题。此外,不需要else中的回声“”。

<?php
    $sql = "SELECT note_id, class, subject, topic, content, author FROM notes WHERE class = 'JHS 2' ORDER BY subject, topic";
    $result = $pdo->query($sql);
    $result->setFetchMode(PDO::FETCH_ASSOC);
    $lastSubject = '';
    while($row=$result->fetch()){
        if(!in_array('subject', $row['subject']) {
            if($lastSubject != $row['subject']) {
?>
<li><a class="collapsible-header waves-effect arrow-r"><i class="fa fa-chevron-right"></i><?php echo $row['subject']?><i class="fa fa-angle-down rotate-icon"></i></a>
<div class="collapsible-body">
<?php
            }
?>
<ul class="list-unstyled">
    <li><a data-id="<?php echo $row['note_id']?>" href="<?php echo $row['note_id']?>" class="waves-effect" id="getUser"><?php echo $row['topic']?></a>
    </li>
</ul>
<?php
            if($lastSubject != $row['subject']) {
?>
</div>
<?php
                $lastSubject = $row['subject'];
            }
?>
</li>
<?php
        }
    }

?>