【问题标题】:PHP: content from the database isn't displayed correctlyPHP:来自数据库的内容未正确显示
【发布时间】:2014-05-15 21:59:32
【问题描述】:

在数据库中,字段看起来不错,但是当此字段显示在 webapp 中时,我从数据库中读取的内容之前会出现空白字符。当我打印查询内容时,此字段显示为没有空白字符。有什么解决这个问题的建议吗?

网络应用上的内容: http://postimg.org/image/bxz6k7yzz/

数据库内容:http://postimg.org/image/siix0jrdf/

我在这里打印内容。

                <div id="algcode">
                    <pre>
                      <code class="language-cpp">
                          <?php echo $query->content; ?>
                      </code>
                    </pre>
                </div>

在模型中:

         $query = $this->db->query($sql2);

         if ($query->num_rows()>0) {
         return $query->row();
         }

在控制器中:

          public function searchAlgorithm ($id) {
              $this->load->model('algorithm');
              $this->load->model('comment');

              $algorithm ['query'] = $this->algorithm->getAlgorithm($id);
              if($algorithm['query']!=false) {
                  $algorithm ['id'] = $this->algorithm->getId($id);
                  $algorithm ['comments'] = $this->comment->getComments($id);

                  $this->loadViews($algorithm);
              } else {
                  $this->loadErrorViews();
              }
          }

【问题讨论】:

  • 您是否尝试过在回显$query-&gt;content 时使用trim()
  • 我得到 FATAL ERROR: Fatal error: Call to a member function trim() on a non-object
  • trim(); 不是真正的对象,所以你可能用错了
  • 没有必要进行修剪,因为@goksiii 已经表明没有空格可以从数据库中修剪。相反,&lt;pre&gt; 包括您的空白和制表符。请看下面我的回答。这就是他们在codefleet 代码格式化程序上使用的东西。

标签: php html sql codeigniter


【解决方案1】:

不要在 &lt;pre&gt; 标签中使用空格

你有:

<pre>
  <code class="language-cpp">
    <?php echo $query->content; ?>
  </code>
</pre>

试试这个:

<pre><code class="language-cpp"><?php echo $query->content; ?></code></pre>

或者:

<pre><code class="language-cpp">
   <?php echo $query->content; ?>
</code></pre>

【讨论】:

  • 很高兴为您提供帮助。请不要忘记将您的问题标记为已回答。 :)
【解决方案2】:

你应该这样使用修剪:

     <div id="algcode">
                    <pre>
                      <code class="language-cpp">
                          <?php echo trim($query->content); ?>
                      </code>
                    </pre>
     </div>

【讨论】:

    【解决方案3】:

    不要在 &lt;code&gt; 标记中使用空格,并使用修剪,如下所示:

    <code class="language-cpp"><?php echo trim($query->content); ?></code>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2020-11-07
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多