php - 显示 1 而不是全部 out 数据库

标签 php html css sql foreach

我的网站(使用 php(pdo) 构建)遇到了这个问题。如果我想从数据库中取出 1 个东西,我会得到所有的东西,而不仅仅是 1 个。正如您所看到的,正在显示 2 个“onderwerp”,但我只想要其中的 1 个,例如第一个。

代码是:

<div class="col-md-6">
 <div class="well dash-box">
   <h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> 12</h2>
    <h5><a href="https://google.nl"> hoi</a></h5>
     <?php
      $toppics = $app->get_topics();
      $i = 4;
      foreach ($toppics as $topic) {
      echo '<li><a href="#section' . $i++ . '">' . $topic['onderwerp'] . '</a></li>';
      }
      ?>
   </div>
 </div>

现在我想让它显示 1 而不是全部,但我没有完成

The 2 things with the dots are out of the database

The database where the things behind the dots came from

 public function get_topics(){
        $getTopic = $this->database->query("SELECT * FROM topics ORDER BY id DESC");
        $this->database->bind(":ledenpagina_id", $_SESSION['ledenpagina_id']);
        $topics = $this->database->resultset();

        return $topics;

}

最佳答案

 <?php
      $toppics = $app->get_topics();
      $i = 4;
      $j = 0;
      foreach ($toppics as $topic) {
      if($j >= 1) continue;
      echo '<li><a href="#section' . $i++ . '">' . $topic['onderwerp'] . '</a></li>';
      $j++;
      }
      ?>

它将跳过第一个迭代之后的所有迭代,并且只会打印第一个值。虽然这不是标准解决方案,但您应该考虑在数据库查询中设置限制 1。但是,如果您不能这样做,这就可以解决问题。

关于php - 显示 1 而不是全部 out 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49454031/

相关文章:

html - 为什么 Highcharts div/chart 会超出父 div?

html - 有没有语义上更好的方法来创建这种圆圈文本布局?

css - 样式扩展 ="true"

javascript - 如何检索具有相同名称的多个输入的值?

php - Laravel 下拉列表

php - 任何人都知道为什么我总是得到重复的图像

javascript - 使选项卡正确对齐

python - 从 CherryPy 提供 css 文件

html - 自定义 header Bootstrap Accordion

javascript - 模型 Laravel 和 Vue 上的自定义方法