获得正确的话题'每条记录

时间:2018-04-03 11:35:31

标签: php html mysql sql

我的问题是我不会得到正确的记录。正如您所看到的,每个主题都有一个board_id,它需要显示正确的主板,但现在它只显示stel jezelf voor,最后一条记录需要nieuws & events

How it looks like now

Klanten db enter image description here

数据库与董事会

主题数据库

显示记录的代码:

<?php
   $toppic = $app->get_topics();
   foreach($toppic as $topic){
    echo '<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><a href="https://tom.lbmedia.nl/reactie"> '.$topic['topicnaam'].'</b></a>'; 
      echo '<a> - ' . $topic['voornaam'] . " " . $topic['achternaam'] . '</a>' ;
      echo '<a style="float:right; margin-top:15px;"> reacties</a> <span style="float:right; color:grey; margin-top:15px"class="fa fa-comment"></span>';
      echo '<hr><a><span class="badge bg-red">' . $board['topic'] . '</span></a>';
      echo '</div></div>';
     }
  ?>

此代码的功能是:

public function get_boards(){
    $getBoards = $this->database->query("SELECT * FROM boards ORDER BY id DESC");
    $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");
    $topics = $this->database->resultset();

    return $topics;

}

总结,我想要的:

我希望它能够以正确的主题展示合适的主板。正如您所看到的,销售页面有board_id 2,因此需要显示&#39; Nieuws&amp;事件&#39;而不是&#39; stel jezelf voor&#39;

EXTRA

如果可能的话,我希望它能使整个记录/ div可以点击。有什么提示/技巧吗?

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试进行一些考虑,如JOIN以下,您可以查看

SELECT boards.topic, topics.topicnaam 
FROM topics 
LEFT JOIN boards ON boards.id = topics.boards_id 
ORDER BY topics.id ASC

如果有一个名为klanten的表格,那么查询就像:

SELECT boards.topic, topics.topicnaam, klanten.foto 
FROM topics 
LEFT JOIN boards ON boards.id = topics.boards_id 
LEFT JOIN klanten ON topics.ledenpagina_id = klanten.ledenpagina_id 
GROUP BY topics.id 
ORDER BY topics.id ASC