PHP数据库输出未显示正确方法 [英] PHP database output not showing the correct way

查看:63
本文介绍了PHP数据库输出未显示正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个主题不在我数据库中的论坛.我的网站是用PHP(PDO)构建的,问题是我无法从数据库中获取主题,而无法在每个不同的块中相互显示正确的方法.

I am making a sort of forum with subjects out of my database. My website is built in PHP (PDO) the problem is that I cant get the subject out of the database to show the correct way under each other in each a different block.

<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函数仍然不能正常工作

I want this also to be each record in a new block. In the code, you can see I tried some stuff but it isn't the right way or its not working. I know the j++ isn't good but if I delete that and the LIMIT 1 function it doesn't still don't work

更新

如果我在答案中使用类似的代码,则它看起来像这样,而不是2个不同的块.

If I use the code like it is in the answer given it looks like this and not 2 diffent blocks.

<?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并在foreach中将if($j >= 1) continue;删除.循环,因为它们都将您的输出限制为仅1个主题.

First of all: Since you want to fetch multiple topics from the DB, you must remove the LIMIT 1 from the query and the if($j >= 1) continue; in the foreach loop, as they both are restricting your output to only 1 topic.

在您的$toppics的foreach循环中(正确的拼写:topics; P),您当前仅回显一个锚标记(链接),但您想要的是(在这里用您的话来说)一个块".无论您希望该块是什么样子,都可以在foreach循环中定义该块.

In your foreach loop for $toppics (correct spelling: topics ;P) you currently only echo an anchor tag (link), but what you want is (to use your words here) a 'block'. Whatever you want that block to look like, the place for defining that is within that foreach loop.

现在我不知道您要使用/想要使用什么元素,类或样式,因此我将举一个包含标题的块的示例,并在其下方提供链接:

Now I don't know what elements, classes or stylings you use / want to use, so I will make an example of a block that consists of a title and below that the link:

//rename $topic keys to the names of your DB columns
foreach($toppics as $topic){
    echo '<div>';
    echo '<h3>'.$topic['title'].'</h3><br>'; 
    echo '<a href="#section'.$topic['id'].'">'.$topic['link_text'].'</a>';
    echo '</div><br>';
}

我知道我的解决方案看起来并不完全像您给定的图像,但是应该可以理解如何以及在何处构建块.

I know my solution will not look exactly like your given image, but it should get the point across how and where you can build your blocks.

当您了解HTML的基础知识时,我认为这个问题应该可以轻松解决,因此,我真的建议您在从事大型项目之前,先学习一些有关HTML的知识.

I think this problem should have been easily solveable when you know the basics of HTML, so I would really recommend you learning a bit more about HTML before you work on big projects.

编辑:
正如我在回答中提到的,my solution will not look exactly like your given image因为I don't know what elements, classes or stylings you use.您剩下的问题现在是正确的html标签,类和样式的用法.

Edit after question was edited:
As I mentioned in my answer, my solution will not look exactly like your given image because I don't know what elements, classes or stylings you use. Your remaining problem is now the usage of the correct html tags, classes and stylings.

似乎已生成的div的父元素的样式设置为您希望单个块的外观.
因此,您可以做的是删除父元素,并将其用作生成的div的替换,就像这样:

It appears that the parent element of the generated divs is styled the way you want the single blocks to look like.
So what you could do is remove the parent element and use it as a replacement of the generated div, like so:

<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;
  foreach($toppics as $topic){
      echo '<div class="well dash-box">';
      echo '<h3>'.$topic['onderwerp'].'</h3><br>'; 
      echo '<a href="#section' . $i++ . '">'  .$topic['omschrijving'].'</a>';
      echo '</div><br>';
  }
  ?>
  <!--</div>-->
  </div>

边注:我不同意您对href属性#section1的构建.在构建这些部分时,您将必须知道先前的foreach循环中的确切索引.而是使用主题本身的某些属性,例如其ID,标题或描述(就像我在第一个代码块中所做的那样).这样,在构建节时,您可以轻松地知道如何设置元素id属性.

sidenote: I do not agree with your building of your href attribute #section1. When building these sections you would have to know that exact index from that previous foreach-loop. Instead, use some attribute from the topic itself, maybe its ID, title, or description (like I did in the first codeblock). This way when you are building the sections you can easily know how to set the elements id attribute.

这篇关于PHP数据库输出未显示正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆