MYSQL:显示正确的记录

时间:2018-04-04 07:16:30

标签: html mysql sql join

我正试图为自己创建一个论坛,所以我可以从中学习。现在我需要它只显示一个具有正确board_id的电路板上的'主题',你可以在第一张图片上看到电路板,第二张图片是带有主题的数据库,所以如果我点击' Nieuws&活动板只需要向我展示topicnaam(topicname)= salespage的记录。

我只是勉强得到这个,我不确定该怎么做。

聚苯乙烯。这是我的第一个问题,请告诉我,如果我做错了或者我错过了一些重要的信息。

boards 的数据库 enter image description here

论坛主题的数据库 enter image description here

**显示主题的代码:**

                <?php


                    $toppic = $app->get_topics();
                    foreach($toppic as $topic){

                        echo '<a href="https://tom.lbmedia.nl/reactie"> <div id="topic">';
                        echo '<div id="topicimg">';
                        if(file_exists('assets/images/profielfotos/'.$topic['klant_id'])) { 
                            echo '<img class="img-circle" src="/assets/images/profielfotos/'.$topic['klant_id'].'/'.$topic['foto'].'" />';
                        } else {
                            echo '<i class="fa fa-fw fa-user img-circle"></i>';
                        }
                        echo '</div><div id="topictekst">';
                        echo '<b>'.$topic['topicnaam'].'</b>'; 
                        echo ' - ' . $topic['voornaam'] . " " . $topic['achternaam'] ;
                        echo '<span style="float:right; margin-top:15px; margin-left:5px;">reacties</span> <span style="float:right; color:grey; margin-top:15px"class="fa fa-comment"></span>';
                        echo '<hr><span class="badge bg-red">' . $board['topic'] . '</span>';
                        echo '</div></div></a>';
                    }
                ?>

我正在使用的功能:

 public function get_boards(){
        $getBoards = $this->database->query("SELECT boards.*, ledenpagina.ledenpagina_id FROM boards 
        LEFT JOIN ledenpagina ON ledenpagina.ledenpagina_id = boards.ledenpagina_id
        ORDER BY id DESC");
//        $this->database->bind(":ledenpagina_id", $_SESSION['ledenpagina_id']);
        $boards = $this->database->resultset();

        return $boards;

    }


    public function get_topics(){
        $getTopic = $this->database->query("
        SELECT topics.*, klanten.foto, klanten.voornaam, klanten.achternaam FROM topics 
        LEFT JOIN klanten ON topics.klant_id=klanten.id
        ORDER BY id ASC");
//        $this->database->bind(":ledenpagina_id", $_SESSION['ledenpagina_id']);
        $topics = $this->database->resultset();

        return $topics;

    }

2 个答案:

答案 0 :(得分:0)

使用Join

以下是一个例子:

SELECT Boards.id, Boards.topic, Topics.board_id, Topics.topicnaam
FROM Boards
INNER JOIN Topics
ON Boards.id=Topics.board_id
WHERE Boards.id = 2;

答案 1 :(得分:0)

您的代码是:

如果您输入了电路板ID:

SELECT t.* FROM topics t
JOIN boards b
    ON t.borad_id = b.id
WHERE b.id = 2

SELECT t.* FROM topics t
WHERE t.borad_id = 2

如果您有输入板描述:

SELECT t.* FROM topics t
JOIN boards b
    ON t.borad_id = b.id
WHERE b.description = 'Nieuws & Events'