【发布时间】:2018-03-27 17:31:58
【问题描述】:
我正在用我的数据库中的主题创建一个论坛。我的网站是用 PHP (PDO) 构建的,问题是我无法将主题从数据库中取出以在每个不同的块中显示正确的方式。
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> Stel jezelf voor</h2>
<h5><a href="https://tom.lbmedia.nl/onderwerp"> Laat wetn wie jij en je business zijn</a></h5>
</div>
</div>
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> 12</h2>
<?php
$toppics = $app->get_topics();
$i = 0;
$j = 0;
foreach ($toppics as $topic) {
if($j >= 1) continue;
echo '<a href="#section' . $i++ . '">' . $topic['onderwerp'] . '</a>';
$j++;
}
?>
</div>
</div>
功能:
public function get_topics(){
$getTopic = $this->database->query("SELECT * FROM topics ORDER BY id DESC LIMIT 1");
$topics = $this->database->resultset();
return $topics;
}
我希望这也成为新块中的每条记录。在代码中,您可以看到我尝试了一些东西,但它不是正确的方法或它不起作用。我知道j++ 不好,但如果我删除它和LIMIT 1 函数它仍然不起作用
更新
如果我使用答案中的代码,因为它看起来像这样,而不是 2 个不同的块。
<?php
$toppics = $app->get_topics();
$i = 0;
foreach($toppics as $topic){
echo '<div>';
echo '<h3>'.$topic['onderwerp'].'</h3><br>';
echo '<a href="#section' . $i++ . '">' .$topic['omschrijving'].'</a>';
echo '</div><br>';
}
?>
【问题讨论】:
-
如果您请求多个主题,为什么要在查询中设置
LIMIT 1? -
正如我在问题中所说的,我可以把它放在一边,所以不要为此烦恼